Nested Switch Statements in C Programming with Real-Life Examples

Nested Switch Statements in C Programming with Real-Life Examples

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.

In this tutorial, we will learn about the syntax of a nested switch statement in C programming. In addition, we shall also write a real-life example to demonstrate the concept of nested switch statements.

What is a nested 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 Informatics
    2. Department of Machine Learning
  2. School of Business
    1. Department of Commerce
    2. Department of Purchasing
  3. School of Engineering
    1. Department of Mechanical Engineering
    2. Department of Mechatronics 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.

Nested Switch Statements in C Program

Let’s now write an actual program to apply the concepts learned.

The program below assumes that only the school of business has departments for the purpose of demonstrating a nested switch statement.

#include<stdio.h>
int main()
{
  int a,b;
  printf("1.School of Computer Science\n");
  printf("2.School of Business\n");
  printf("3.School of Engineering\n");
  printf("make your selection\n");
  scanf("%d",&a);
  switch (a)
  {
    case 1: 
      //code to be executed 
      //if school of computer science is chosen;
      break;
    case 2: 
      //code to be executed 
      //if school of business is chosen;
      printf("Available Departments\n");
      printf("1.Department of commerce\n");
      printf("2.Department of purchasing\n");
      printf("Make your selection.\n");
      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("You chose Department of commerce\n");
        break;
        case 2: 
        // code to be executed if b = 2;
        printf("You chose Department of purchasing");
        break;
      }	  
      break;
  }
}

Note: You can also write the C code for the same example using the if-else statement in programming.

The output of the Program:

In the above code, if a user chooses option 2(school of business), a list of departments in the school will be displayed and a user asked to choose a department. On choosing a department the user will get an output showing the department chosen.

The output is shown below.

nested switch statements c output

Try running this program on your system and choose various options. This will help you understand each line of code.

What’s Next?

Check out the complete C/C++ programming tutorial.

This is all about nested switch statements in C programming. I tried explaining with a real-life example and a code.

Any doubt? Any questions related to nested switch Programming Examples in C/C++? Let’s share your thought in the comment section.

Happy Programming!

10 Comments

  1. Thanks for these coding examples and practical. It’s easy to learn with these practical examples.

  2. Please I couldn’t find any default case codes. For example, if the user chooses an invalid option what should the program execute?

    1. Adrew, you can use the default keyword to execute the default code block.

      Here is the default statement you can add in the above example.

            switch(b) 
            { 
              case 1:
              // code to be executed if b = 1;
              printf("You chose Department of commerce\n" );
              break;
              case 2: 
              // code to be executed if b = 2;
              printf("You chose Department of purchasing" );
              break;
              default:
              printf("Invalid selection.");
              break;
            }  
      
      1. Thank you so much am a student from Kenya 🇰🇪 I follow your lectures alot more so on coding ,, as they are precise

  3. As Everyone said…

    The examples with explanations are NICE…!!!

    Now I am able to understand the switch statement…

    Eagerly looking forward to even more explanations….👍👍

  4. Hi,

    Good Morning,

    Respected: SIR.

    You have given the example of Departments in that its only School of business is executed. But, I need both three of the Departments to get executed and if you don’t mind please make it 4 departments and make execute all departments one by one. Please…..

    Because I need to be executed…

  5. Thank you so much am a student from Kenya 🇰🇪 I follow your lectures a lot more so on coding as they are precise.

Leave a Reply

Your email address will not be published. Required fields are marked *