C# Switch Statements
Use the switch
statement to select one of many code blocks to be executed.
Syntax
switch(expression) { case x: // code block break; case y: // code block break; default: // code block break; }
Use the switch
statement to select one of many code blocks to be executed.
switch(expression) { case x: // code block break; case y: // code block break; default: // code block break; }
Instead of writing many if..else
statements, you can use the switch
statement.
The switch
statement selects one of many code blocks to be executed:
switch (expression) { case x: // code block break; case y: // code block break; default: // code block }
This is how it works :
switch
expression is evaluated oncecase
break
statement breaks out of the switch block and stops the executiondefault
statement is optional, and specifies some code to run if there is no case match