The switch statement serves as a multiway branching tool, facilitating the redirection of program flow based on the value of the expression. This feature allows for efficient control transfer to various sections of code depending on the expression's value. The switch statement acts as a control expression, empowering the alteration of execution flow based on specified values.
Syntax:
It has the following syntax:
switch (num)
{
case 1: // if the value of num = 1;
break;
case 2: // if the value of num = 2;
break;
default: // if the number does not belong to any of the case
}
Consider the following when using Switch Case:
- A switch statement's expression has to be of an integral or characters type or a class type with a single converting function to an integral or character type.
- A switch can contain a limitless amount of case statements. The value being compared against and a colon follow each case.
- When the value of the variable being turned on equals a case, the commands that follow that case will be executed until a break clause is reached.
- When a break statement arrives, the switch is terminated, and control is transferred to the line after the switch statement.
- A break is not required in every situation. If no break appears, control will flow through to consecutive cases until a break is reached, i.e. all case statements will be executed as soon as the compiler determines a comparison to be true.
- A switch statement can include an optional default case , which must conclude. When none of the other situations are true , the default case can be used to complete a task. In the default example, no break is required.
Nested Switch
The switch statement, just like other conditional statements such as if and else, allows for nesting. This means that we can have a switch statement inside another switch statement, creating a continuous flow of execution. Let's examine the example below to illustrate this concept clearly.
Syntax:
It has the following syntax:
switch(num)
{
// if the value of num = 1;
case 1:
// The Nested switch
switch(number)
{
// if the value of number = 10
case 10:
statement 1;
break;
//if the value of number = 20
case 20:
statement 2;
break;
//if the value of number=30
case 30:
statement 3;
break;
// code to be executed if the value of the number does not match any case
default:
}
break;
// if the value of num=2;
case 2:
statement 2;
break;
//if the value of num=3;
case 3:
statement 3;
break;
//code to be executed if the value of the number does not match any case
default:
}
Filename: NestedSwitch.c
// program for demonstrating the nested switch case
#include <stdio.h>
int main()
{
int x1 = 1, y1 = 2;
// the Outer Switch case
switch (x1) {
// if the value of x ==1
case 1:
//enters into the nested switch
switch (y1) {
// if the value of y is 2
case 2:
printf( "The chosen choice is 2");
break;
// if the value of y is 3
case 3:
printf("The chosen choice is 3");
break;
}
break;
// if the value of x ==4
case 4:
printf( "The chosen choice is 3");
break;
// if the value of x ==5
case 5:
printf("The chosen choice is 3");
break;
default:
printf( "The Choice is other than 1, 2 3, 4, or 5");
}
return 0;
}
Output:
The chosen choice is 2
Explanation:
The software processes a pair of variables, x1 and y1, and employs the content of x1 to determine the outer switch condition. When x1 equals 1, the value of y1 undergoes additional assessment within an embedded switch block.
In this particular case:
- If x1 is 1 and y1 is 2, the message "The chosen choice is 2" is printed.
- If x1 is 1 and y1 is 3, the message "The chosen choice is 3" is printed.
- If x1 is 4, it prints "The selected option is 3" .
- If x1 is 5, it outputs "The selected option is 3" .
- If x1 has any other number, it writes, "The Choice is other than 1, 2, 3, 4, or 5" .
The external switch statement checks the value of x1 and, based on the different cases, either triggers the inner switch statement or proceeds directly to the default case if x1 is not equal to 1, 4, or 5. In the nested switch, the program evaluates the value of y1 and displays the corresponding message.
Advantages of Nested Switch Statements:
There are several advantages of Nested Switch Statements . Some main advantages of Nested Switch Statements are as follows:
- Decision Making: Nested switch statements enable hierarchical decision-making, allowing the computer to assess multiple circumstances in an organized manner.
- Readability: In certain complex cases, nested switches can improve code readability. Proper indentation and organization can help identify which situations are affected by external factors.
- Modularity: Nested switching can be used to create code with modular frameworks, particularly when dealing with multi-step procedures with specific scenarios to handle at each phase.
Disadvantages of Nested Switch Statements:
There are several disadvantages of Nested Switch Statements . Some main disadvantages of Nested Switch Statements are as follows:
- Complexity: As the number of branching increases, the code can become complicated and hard to understand. This complication may result in mistakes during development and maintenance.
- Difficulties with Debugging: Debugging nested switches can be difficult, especially when attempting to trace control across numerous levels of nested cases.
- Limited Applicability: Nested switches may sometimes be the most efficient or simple option. Other control frameworks, such as if-else statements or polymorphism (in Object-Based programming), can result in more understandable and maintained code in many circumstances.
- Maintenance Issues: When improvements are required, making improvements can be error-prone and could result in bugs.
- Performance: While current compilers have been designed to handle nested switches quickly, deeply nested switches may influence program speed. However, this impact is normally negligible in most circumstances.