“switch” is a case control structure.
It contains case and default values.
“switch” statement takes a value as a expression for equality testing against a list of values/case.
syntax for a switch statement:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | switch(expression) { case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); } |
Rules of switch statement −
In a “switch” statement we can have any number of case.
for equality testing against a list of values/case, constant-expression for a case must be the same data type as the variable in the switch expression.
When “switch” expression value is equal to a case, the statements following that case will execute until a break statement is reached/executes.
When a break statement is reached/executes, the switch statement terminates, and the control jumps to the outside of switch.
a break is a optional. If there is no break in the switch case, the flow of control will jump to the next case of switch until a break is reached.
A switch statement also have a default case, which appear at the end of the switch. when none of the cases is true then default case will execute. No break is needed in the default case. “default” case is also an optional.
Example: Write a program to take one numbers from user between 1 to 3. Write entered number in words if number between 1 to 3 otherwise print wrong number using switch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> using namespace std; int main() { int n; cout << " enter number between 1 to 3"; cin >> n; // 3 enter by user switch (n) { case 1: cout << "\n one"; break; case 2: cout << "\n two"; break; case 3: cout << "\n three"; break; default: cout << "\n wrong number"; } return 0; } |
Output
1 2 3 | enter number between 1 to 3 3 // user entered 3 in variable n three |
Note: In the above program, output depends on user input
Example: Write a program using switch statement but without break.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; int main() { char c = 'B'; switch (c) { case 'A': cout << "Excellent!\n"; break; case 'B': case 'C': cout << "Well done\n"; case 'D': cout << "You passed\n"; break; case 'F': cout << "Better try again\n"; break; default: cout << "Invalid Character \n"; } return 0; } |
Output
1 2 3 | Output: Well done You passed |
Description: Because if there is no break in the switch case, the flow of control will jump to the next case of switch until a break is reached.
Note: In the above program, for example value of char variable c = ‘ A’ then the output will be as follows:
Output
1 | Excellent! |
Nested switch statements
In C++ programming language we can define switch statement within another switch.
Syntax for a nested switch statement: −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | switch(expression1) { case 'A': printf("case A of outer switch" ); switch(expression2) { case 'A': printf("case A of inner switch" ); break; case 'B': /* case code */ } break; case 'B': /* case code */ } |