Nested Switch Case in C Programming

  • Nested Switch statements refers to Switch statements inside of another Switch Statements.
  • The first switch is referred to as an outer switch statement whereas the inside switch is referred to as an inner switch statement.

Syntax

Switch (expression)
{
case value1:
//Statements

Switch (expression)
{
case value1:
//Statements
break;
case value 2:
//Statements
break;
Default:
//Statements
}
break;
case value 2:
//Statements
break;

case value n:
//Statements
break;
Default:
//Statements
}

For Example,

Write a c program to find the course according to given stream

#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b,c;
 
  printf(\"1.Sciecne\\n\");
  printf(\"2.Commerce\\n\");
  printf(\"Select Your Stream to get Course offered for you\\n\");
  scanf(\"%d\",&a);
  switch (a)
  {
    case 1: 
      //code to be executed 
      //if school of  science is chosen;
      
     printf(\"Available Course for science students are as follow\\n\");
      printf(\"1.Web Designing\\n\");
      printf(\"2.Web Developer\\n\");
      printf(\"3.Software Developer\\n\");
      printf(\"Make your selection.\\n\");
      scanf(\"%d\",&b);
 
      //inner switch to display job available 
      //under the  commerce
      switch(b) 
      { 
        case 1:
        // code to be executed if b = 1;
        printf(\"You chose Web Designing \\n You can create web site\\n\" );
        break;
        case 2: 
        // code to be executed if b = 2;
         printf(\"You chose Web Developer \\n You can  create websites as well develop websites\\n\" );
        break;
        case 3: 
        // code to be executed if b = 2;
         printf(\"You chose Software Developer \\n You can  work with Software development department\\n\" );
        break;
      }
      break;
    case 2: 
      //code to be executed 
      //if  commerce is chosen;
      printf(\"Available Course for Commerce students are as follow\\n\");
      printf(\"1.Basic Tally\\n\");
      printf(\"2.Advance tally and GST\\n\");
      printf(\"3.K_AOAFA Course\\n\");
      printf(\"Make your selection.\\n\");
      scanf(\"%d\",&c);
 
      //inner switch to display job available 
      //under the  commerce
      switch(c) 
      { 
        case 1:
        // code to be executed if c = 1;
        printf(\"You chose basic tally \\n So have to join advance tally to work with account department\\n\" );
        break;
        case 2: 
        // code to be executed if c = 2;
         printf(\"You chose advance tally \\n You can  work with account department\\n\" );
        break;
        case 3: 
        // code to be executed if c = 2;
         printf(\"You chose K_AOAFA Courses \\n You can  work with account department and back office\\n\" );
        break;
      }   
      break;
      default:
      	printf(\"invalid selection \");
  }
  getch();
}