Lesson 8: Learning to work with the Switch command in #C

Lesson 8: Learning to work with the Switch command in #C. Learn how to work with the Switch command in C #: The Switch command acts like a series of if statements.

The Switch command is actually a list of Possible modes for which each command has a command or code to execute.

This command It also has a default state that will execute if none of its states are true. A simple Switch command has a structure similar to the example code below.

int number = 1; switch(number) { case 0: Console.WriteLine(“The number is zero!”);
break; case 1: Console.WriteLine(“The number is one!”); break; }.

The value of the identifier (the value of the variable that we want the Switch command to act on) after
We put the keyword Switch. Then a list of different types of modes for that value by each command
The case is specified and the value of the identifier is compared to the value of each case.

If the value of the ID with the value of each Was not equal, the commands of the default section are executed.

If you are careful, a break statement is placed at the end of each case.

for what? If the value of one
case is equal to the value of the switch command ID, the commands of that case are executed and if at the end of the case the command
If there is no break, the next case commands will be executed.

In other words, the break command, Stops the process of executing Switch commands and the program sends it to the code line after the Switch. In writing break Be careful, ???? Writing it can disrupt the program call.

When you specify a function in a case statement, you can use the return output of the function
Return as Switch output.

In the example code, we used an integer numeric variable as the switch command ID, but this
The identifier can be a String text type or any other C # data type.

In the following example, we first take a value as input, then use it as a text variable. We sent the string to the Switch command. In the example code below, a command for both “yes” and “Maybe” values
We have determined the subscriber.

Console.WriteLine("Do you enjoy C# ? (yes/no/maybe)");
string input = Console.ReadLine();
switch(input.ToLower())
{
 case "yes":
 case "maybe":
 Console.WriteLine("Great!");
 break;
 case "no":
 Console.WriteLine("Too bad!");
 break;
}

In the example code above, the user is asked a question and asked to enter one of the values ​​”yes”, “no” and “maybe”. Enter.

The value entered from the input is then read by the ReadLine.Consale () command and a command
The switch is based on that.

For the convenience of the user, input characters are entered by the ToLower () command We have converted the user to lowercase letters so that when comparing the values ​​of the cases, there is a difference between the values ​​entered Do not exist for uppercase and lowercase letters.

But without the default command on the switch, if the user in the example above has an unspecified value for When the item is entered, the program will have no output.

So the above example code along with the default form We rewrite the following:

Console.WriteLine("Do you enjoy C# ? (yes/no/maybe)");
string input = Console.ReadLine();
switch(input.ToLower())
{
 case "yes":
 case "maybe":
 Console.WriteLine("Great!");
 break;
 case "no":
 Console.WriteLine("Too bad!");
 break;
 default:
 Console.WriteLine("I'm sorry, I don't understand that!");
 break;
}

If the user enters a value other than the values ​​specified for the cases, the default field is by default
will be executed.

%d bloggers like this: