switch case in C Programming Language
Table of Content:
The if statement in C, makes selections based on a single true or false condition. But switch case have multiple choice for selection of the statements or we can switch case is a multiway branch statement. The C switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. It adds an easy way to dispatch execution to different parts of your code based on the value of an expression.
switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; . . . case valueN: // statement sequence break; default: // default statement sequence }
switch case flow chart
The expression must be of type byte, short, int, or char;
each of the values specified in the case statements must be of a type compatible with the expression.
Examples of Switch case:
// Program to implement the sign function #includevoid main() { int number=20; switch(number){ case 10: printf("case 10 \n"); break; case 20: printf("case 20 \n"); break; case 30: printf("case 30 \n"); break; default: printf("Not in 10, 20 or 30 \n"); } }
case 20 Press any key to continue . . .
Explanation of the above program:
each time through the loop, the statements associated with the case constant that matches i are executed. All others are bypassed. After i is greater than 3, no case statements match, so the default statement is executed.
The switch statement works like this:
The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.
Why break
is necessary in switch statement ?
The break statement is used inside the switch to terminate a statement sequence. When
a break statement is encountered, execution branches to the first line of code that follows the
entire switch statement. This has the effect of jumping out of the switch.
The break statement is optional. If you omit the break, execution will continue on into the
next case. It is sometimes desirable to have multiple cases without break statements between
them.
Examples of Switch case:
// A simple example of the switch. // Program to implement the sign function #includevoid main() { int i; for(i=0; i<6; i++) switch(i) { case 0: printf("This is case zero.\n"); break; case 1: printf("This is case one.\n"); break; case 2: printf("This is case two.\n"); break; case 3: printf("This is case three.\n"); break; default: printf("This is greater than 3.\n"); } }
This is case zero. This is case one. This is case two. This is case three. This is greater than 3. This is greater than 3. Press any key to continue . . .
Examples of Switch case: The break statement is optional.
// In a switch, break statements are optional. // Program to implement the sign function #includevoid main() { int n; for(n=0; n<12; n++) switch(n) { case 0: case 1: case 2: case 3: case 4: printf("n is less than 5 \n"); break; case 5: case 6: case 7: case 8: case 9: printf("n is less than 10 \n"); break; default: printf("n is 10 or more \n"); } }
n is less than 5 n is less than 5 n is less than 5 n is less than 5 n is less than 5 n is less than 10 n is less than 10 n is less than 10 n is less than 10 n is less than 10 n is 10 or more n is 10 or more Press any key to continue . . .
Nested switch Statements
Realistic usage of switch statement
// Program to implement the sign function #includevoid main() { int count = 1; int target = 1; switch(count) { case 1: switch(target) { // nested switch case 0: printf("target is zero inner switch \n"); break; case 1: // no conflicts with outer switch printf("target is one inner switch \n"); break; } break; case 2: printf("case 2 outer switch \n"); } }
target is one inner switch Press any key to continue . . .