“switch case ” 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.
Switch case statement is used in programming to select one amont multiple options.
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 | #include <stdio.h> int main() { int n; printf(" enter number between 1 to 3"); scanf("%d", &n); switch (n) { case 1: printf("\n one"); break; case 2: printf("\n two"); break; case 3: printf("\n three"); break; default: printf("\n wrong number"); } return 0; } </stdio.h> |
OUTPUT
1 2 3 | enter number between 1 to 3 2 // user entered 2 two |
Note: In the above program, for example user enters a number 6 then output will be as follows:
1 2 3 | enter number between 1 to 3 6 // user entered 6 wrong number |
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 | #include <stdio.h> int main() { char c = 'B'; switch (c) { case 'A': printf("Excellent!\n"); break; case 'B': case 'C': printf("Well done\n"); case 'D': printf("You passed\n"); break; case 'F': printf("Better try again\n"); break; default: printf("Invalid Character \n"); } return 0; } |
OUTPUT
1 2 | 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:
1 | Excellent |
Nested switch case
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 17 | switch (ch1) { case constant - expression: printf("case A of outer switch"); switch (ch2) { case constant - expression: printf("case A of inner switch"); break; case constant - expression: /* case code */ } break; case 'B': /* case code */ } |