Nested Switch

Written By -

Nested Switch in C:

We can also use nested switch i.e. switch inside a switch.

If the case constants of both inner and outer switch have same value, no problem will arise.

The syntax of the nested switch case in c is as follows:

switch(expression 1)
 {
  Case 1:
    printf(“Outer Switch”);
switch(expression 2)
{
  Case 1:
  printf(“Inner Switch”);
  Break;
  Case 2:
  statements;
}
break;
Case 2:
statements;
}

Example of Nested Switch:

#include &ltstdio.h>
int main () {

   int a = 10;
   int b = 20;
 
   switch(a) {
   
      case 10: 
         printf(“Outer switch\n”, a );
      
         switch(b) {
            case 20:
               printf(“Inner switch\n”, a );
         }
   }
   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

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019