How to implement selection Control in C #
This article mainly introduces how to achieve selection control in C#, which is very detailed and has a certain reference value. Friends who are interested must finish reading it!
The process control of C# is basically the same as other languages, including:
◆ selection control: if, else, switch, case
◆ cycle control: while, do, for, foreach
◆ jump statements: break, continue
◆ exception handling: try, catch, finally
In the following process, we learn more about the C# selection control.
C# selection Control:
Let's do a simple user authentication, as follows:
Public static void Main () {Console.WriteLine ("Please enter your name"); string username = Console.ReadLine (); if (username! = "") / format: if (condition) {/ / Code to run when the condition is met ("Great {0} Welcome to the Matrix!", username) } the code Console.WriteLine that runs when else// does not meet the {/ / conditions ("you have not entered anything, please leave!");}}
We found that as long as you enter a user, you can log in to the system, so let's set the limit and let the specified person log in, so:
Public static void Main () {Console.WriteLine ("Please enter your name"); string username = Console.ReadLine (); if (username = = "jianle") / / format: if (condition) {/ / Code to run when the condition is met ("Great {0} Welcome to the Matrix!", username) } else if (username = = "boss") {/ / second condition Console.WriteLine ("Great {0} Welcome to the matrix!", username);} else {/ / the code to run when the condition is not met ("you have not entered anything, please leave!");}}
Switch selection control
One day, we need to be able to get more people to log on to the matrix, so we can do this:
Public static void Main () {Console.WriteLine ("Please enter your name"); string username = Console.ReadLine (); switch (username) {case "jianle": Console.WriteLine ("Great {0} host welcomes you to the matrix!", username); break; case "boss": Console.WriteLine ("Great {0} boss welcomes you to the matrix!", username); break Case "cctv": Console.WriteLine ("Great Mr. {0} Welcome to the matrix!", username); break; case "gril": Console.WriteLine ("Great Ms. {0} welcome to the matrix!", username); break; default: Console.WriteLine ("you typed incorrectly, please leave"); break }} above is all the content of the article "how to implement selection Control in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!