Nested switch statement
switch statement in C# allows checking a variable/value with a list of values (cases) and executing the block associated with that case.
When we use switch statement within another switch statement (a case statement(s)) i.e. switch statement within another switch statement, we can say it is an example of a nested switch statement.
Synatx:
//outer switch
switch(variable/expression)
{
case <case_value1>:
statement(s);
//inner switch
switch(variable/expression)
{
case <case_value1>:
statement(s);
break;
case <case_value2>:
statement(s);
break;
default:
statement(s);
break;
}
break;
case <case_value2>:
statement(s);
break;
default:
statement(s);
break;
}
C# code for nested switch statement
Here, we have 3 cases:
(Case 1) Using another switch statement, that will give the color name based on the user input (color code – example "R/r" for "Red", "G/g" for "Green", ...)
(Case 2) and Case 3) will print simple message.
Output