C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

C - nested switch statements

Nested Switch Statements occur when a switch statement is defined inside another switch statement. The first switch is referred to as an outer switch statement whereas the inside switch is referred to as an inner switch statement.

A nested switch statement is defined as having a switch statement within another switch statement

The syntax for Nested Switch Case:

The basic syntax used for creating a nested switch statement is as follows:

switch (a)

{

  printf("This a is part of outer switch" );

  case 1: // code to be executed if a = 1;

    break;

  case 2:

    switch(b)

    {

      case 1:

        // code to be executed if b = 1;

        printf("This b is part of inner switch" );

        break; 

     case 2:

        // code to be executed if b = 2;

        printf("This b is part of inner switch" );

        break;

    }

      break;

    default:

      // code to be executed if

      // a doesn't match any cases

}

In the above syntax, we have declared two nested switch statements.

The first switch statement with variable “a” is termed as the outer switch statement. Whereas, the switch statement with the variable “b” is termed the inner switch statement.

Nested Switch Statement Example:

To help you understand the nested switch statement better, let’s consider an example.

You are searching for a department in a university and you’re asked to select a school from a choice of three schools namely:

  1. School of Computer Science
  2. School of Business
  3. School of Engineering

Having selected a school you are again provided with a list of departments that fall under the department namely:

  1. School of Computer Science
  1. Department of IT
  2. Department of Machine Learning
  1. School of Business
  1. Department of Commerce
  2. Department of Management
  1. School of Engineering
  1. Department of Mechanical Engineering
  2. Department of Electrical & Electronics Engineering

This is a good example of the nested switch statement.

The initial choices for Computer Science, Business, and Engineering schools would be inside as a set of switch statements. Then various departments would then be listed within inner switch statements beneath their respective schools.

Program:

#include<stdio.h>

int main()

{

  int a,b;

  printf("------- MAIN MENU --------\n");

  printf("1.School of Computer Science\n");

  printf("2.School of Business\n");

  printf("3.School of Engineering\n");

  printf("Enter your Choice : ");

  scanf("%d",&a);

  switch (a)

  {

    case 1:

      //code to be executed

      //if school of computer science is chosen;

      printf("\nAvailable Departments\n");

      printf("1.Department of IT \n");

      printf("2.Department of Machine Learning \n");

      printf("Enter your Choice : ");

      scanf("%d",&b);

     

       //inner switch to display the departments

      //under the school of Computer Science

      switch(b)

      {

        case 1:

        // code to be executed if b = 1;

        printf("Department of IT - Selected \n");

        break;

        case 2:

        // code to be executed if b = 2;

        printf("Department of Machine Learning - Selected \n");

        break;

      }        

     

      break;

    case 2:

      //code to be executed

      //if school of business is chosen;

      printf("\nAvailable Departments\n");

      printf("1.Department of Commerce\n");

      printf("2.Department of Management\n");

      printf("Enter your Choice : ");

      scanf("%d",&b);

      //inner switch to display the departments

      //under the school of commerce

      switch(b)

      {

        case 1:

        // code to be executed if b = 1;

        printf("Department of Commerce - Selected\n");

        break;

        case 2:

        // code to be executed if b = 2;

        printf("Department of Management - Selected");

        break;

      }        

      break;

         case 3:

      //code to be executed

      //if school of computer science is chosen;

      printf("Available Departments\n");

      printf("1.Department of Mechanical Engineering\n");

      printf("2.Department of Electrical & Electronics Engineering\n");

      printf("Enter your Choice : ");

      scanf("%d",&b);

       //inner switch to display the departments

      //under the school of Engineering

      switch(b)

      {

        case 1:

        // code to be executed if b = 1;

        printf("Department of Mechanical Engineering - Selected\n");

        break;

        case 2:

        // code to be executed if b = 2;

        printf("Department of Electrical & Electronics Engineering - Selected");

        break;

      }        

      break;

  }

}

OUTPUT:

------- MAIN MENU --------

1.School of Computer Science

2.School of Business

3.School of Engineering

Enter your Choice : 1

Available Departments

1.Department of IT

2.Department of Machine Learning

Enter your Choice : 1

Department of IT – Selected