Switch Statement

Statement switch allows a variable to be tested for equality against a list of values and perform various pieces of code depending on the value of that variable.

Syntax of a switch statement:

 switch(expression1) { case constant1: statements1; case constant2: statements2; // и т.д. default: statements3; } 

"statements1" will be executed when the values "expression1" and "constant1" match, then "statement2" and other statements will be executed. The default case is optional and is designed to execute the code in a situation when the value of "exprression1" does not match any constant.

If it is necessary to abort the switch immediately after "operator1" , "statement2" etc., the break statement must be added after the last command for each group of statements.

 switch(expression1) { case constant1: statements1;break; case constant2: statements2;break; // и т.д. default: statements3; } 


 string b=""; int a=3; switch (a) { case 1: b+="one ";break; case 2: b+="two ";break; case 3: b+="three ";break; case 4: b+="four ";break; case 5: b+="five "; case 6: b+="six "; default: b+="other number"; } System.Print(""+b); 



When a is equal to 5, the value of b will be equal to "five six other number"