The use of switch statement inside another switch statement is called nested switch statement.
Syntax
Syntax of nested switch
statement in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | switch(ch1) { case 'A': printf("This A is part of outer switch" ); //use of another switch statement switch(ch2) { case 'A': printf("This A is part of inner switch" ); break; case 'B': /* case code */ } break; case 'B': /* case code */ } |
Example of nested switch statements in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <stdio.h> int main() { //local variable int a = 10; int b = 20; switch (a) { case 10: printf("The OUTER Switch.\n", a); //INNER Switch switch (b) { case 20: printf("The INNER Switch.\n", a); } } return 0; } |
The output of nested switch statements in C.
The OUTER Switch.
The INNER Switch.