C – nested switch statement

The use of switch statement inside another switch statement is called nested switch statement.

Syntax

Syntax of nested switch statement in C:

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:

#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.