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.
A nested switch statement is defined as having a switch statement within another switch statement
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.
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:
Having selected a school you are again provided with a list of departments that fall under the department namely:
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.
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.
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.
Try running this program on your system and choose various options. This will help you understand each line of code.
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!
Thanks for writing this guide. Your way of writing is very easy to understand.
Thanks for these coding examples and practical. It’s easy to learn with these practical examples.
You’re welcome, Vishal!
Please I couldn’t find any default case codes. For example, if the user chooses an invalid option what should the program execute?
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.Thank you so much am a student from Kenya 🇰🇪 I follow your lectures alot more so on coding ,, as they are precise
I’m glad you enjoy reading our tutorials. Thanks for your kind words! It inspires us to share more.
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….👍👍
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…
Thank you so much am a student from Kenya 🇰🇪 I follow your lectures a lot more so on coding as they are precise.