
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. Note: You can also write the C code for the same example using if-else statement in programming. Printf("You chose Department of purchasing" ) Printf("You chose Department of commerce\n" ) inner switch to display the departments if school of computer science is chosen

Printf("1.School of Computer Science\n") The program below assumes that only the school of business has departments for the purpose of demonstrating a nested switch statement. Let’s now write an actual program to apply the concepts learned. Then various departments would then be listed within inner switch statements beneath their respective schools. The initial choices for Computer Science, Business and Engineering schools would be inside as a set of switch statements. This is a good example of the nested switch statement.

Having selected a school you are again provided with a list of departments that fall under the department namely: You are searching for a department in a university and you’re asked to select a school from a choice of three schools namely: To help you understand the nested switch statement better, let’s consider an example. The first switch statement with variable “a” is termed as the outer switch statement. Whereas, the switch statement with the variable “b” is termed as the inner switch statement. In the above syntax, we have declared two nested switch statements.

Printf("This b is part of inner switch" ) Printf("This a is part of outer switch" ) The basic syntax used for creating a nested switch statement is as follows: switch (a)

What is a nested switch statement?Ī nested switch statement is defined as having a switch statement within another switch statement The syntax for Nested Switch Case: In addition, we shall also write a real-life example to demonstrate the concept of nested switch statements. In this tutorial, we will learn about the syntax of a nested switch statement in C programming. The first switch is referred to as an outer switch statement whereas the inside switch is referred to as an inner switch statement. Nested Switch Statements occurs when a switch statement is defined inside another switch statement.
