Nested Switch Statement in C
In this article you will learn about the Nested Switch statement in C programming and how it can be used in a C program.
Nested Switch Statement in C
As the name says Nested-Switch statements is nothing but the Switch statement inside another Switch Statement. The first switch is known as an outer switch statement whereas the inside switch is the inner switch statement.
The Nested Switch statement is used when you have to choose a option inside another option. The outer switch will be a set of options and the inner switch is the set of options given inside the options in outer switch.
Syntax:
switch(char)
{
case 'X':
printf("outer switch" );
switch(char1)
{
case 'X':
printf("inner switch" );
break;
case 'Y': /* code
}
break;
case 'Y': /* code
}
Example
#include <stdio.h>
int main ()
{
int x = 10;
int y = 20;
switch(x)
{
case 10:
printf("outer switch\n", x );
switch(y)
{
case 20:
printf("inner switch\n", x );
}
}
printf("value of x is : %d\n", x);
printf("value of y is : %d\n", y);
return 0;
}
Output
outer switch
inner switch
value of x is : 10
value of y is : 20