Switch statement
The switch statement is
like a set of if statements. It's a list of possibilities, with an action for
each possibility, and an optional default action, in case nothing else
evaluates to true, so we can say that A switch statement allows a variable to be
tested for equality against a list of values.
Syntex: -
|
switch (
CaseVariable )
{
case constantExpression_1:
statements;
break;
case constantExpression_2:
statements;
break;
case constantExpression_3:
statements;
break;
default:
statements;
break;
}
|
Example of switch
statement:-
|
public class
switchStatementExample
{
static void
Main(string[] args)
{
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
case 'C':
Console.WriteLine("Well
done");
break;
case 'D':
Console.WriteLine("You
passed");
break;
case 'F':
Console.WriteLine("Better
try again");
break;
default:
Console.WriteLine("You
Failed!");
break;
}
}
}
|