Nested Switch in C
Written by
Nested Switch in C
In C++, we can use a nested switch statement, where a switch statement is placed inside another switch statement.
If the case constants of both the inner and outer switch statements have the same value, there will be no issues with the code.
The syntax of the nested switch case in c is as follows:
switch (expression1) {
case 1:
printf("Outer Switch");
switch (expression2) {
case 1:
printf("Inner Switch");
break;
case 2:
statements;
}
break;
case 2:
statements;
}
C program for Nested switch case
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
switch (a) {
case 10:
printf("Outer switch\n");
switch (b) {
case 20:
printf("Inner switch\n");
}
}
printf("Value of a is: %d\n", a);
printf("Value of b is: %d\n", b);
return 0;
}
Output
Outer switch
Inner switch
Value of a is :10
Value of b is :20