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.Sciecnen");
  printf("2.Commercen");
  printf("Select Your Stream to get Course offered for youn");
  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 follown");
      printf("1.Web Designingn");
      printf("2.Web Developern");
      printf("3.Software Developern");
      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 siten" );
        break;
        case 2: 
        // code to be executed if b = 2;
         printf("You chose Web Developer n You can  create websites as well develop websitesn" );
        break;
        case 3: 
        // code to be executed if b = 2;
         printf("You chose Software Developer n You can  work with Software development departmentn" );
        break;
      }
      break;
    case 2: 
      //code to be executed 
      //if  commerce is chosen;
      printf("Available Course for Commerce students are as follown");
      printf("1.Basic Tallyn");
      printf("2.Advance tally and GSTn");
      printf("3.K_AOAFA Coursen");
      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 departmentn" );
        break;
        case 2: 
        // code to be executed if c = 2;
         printf("You chose advance tally n You can  work with account departmentn" );
        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 officen" );
        break;
      }   
      break;
      default:
      	printf("invalid selection ");
  }
  getch();
}
Scroll to Top