Let us implement a switch statement in C programming and understand how a switch loop works in C programming with its algorithm, flowchart, example and explanation.
When there are many alternatives and one needs to be selected, we generally tend to use an If-Else statement. However, the complexity increases when there’s a really large set of alternatives.
The C library provides a multiway decision statement known as the switch statement. It validates the value of a given expression against the list of case values and when a match is found, the block of statements associated with that switch case is executed.
Switch Statement Flowchart

Switch Case Syntax
1 2 3 4 5 6 7 8 9 10 11 | switch(test-expression) { case value 1: statement; break; case value 2: statement; break; case value 3: statement; break; default: statement; break; } |
The switch expression is an integer expression or characters. It is validated with all the switch case labels within the switch block.
The switch expression is compared with the case statements and if the case statement does not match it shifts on to the next case label.
If any of the case statement matches the expression, the block of statements gets executed and the program control exits from the switch statement due to the break statement at the end.
The break statement is optional. It signals the end of a switch case and causes an exit from the switch statement. If it is not present, all the successive case labels will be executed till it does not encounter a break statement.
The default case is an optional case. It will be executed if none of the case statements validates with the test expression. There can be at most one default case and it can be placed at any position within the switch block.
Rules For Switch Statement
- The switch expression must be an integral type.
- The break statement transfers the control out of the switch statement.
- The switch case labels must end with a colon.
- The case labels must be constants or constant expressions.
- The case labels must be unique. No two labels can have the same value.
- The American National Standards Institute (ANSI C) allows using a maximum of 257 case labels.
Note: This example of switch statement in C programming is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.
Switch Statement Example in C Programming
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<stdio.h> int main() { int num; printf("\nEnter a Number below 5:\t"); scanf("%d", &num); switch(num) { case 1: printf("You've entered 1\n"); break; case 2: printf("You've entered 2\n"); break; case 3: printf("You've entered 3\n"); break; case 4: printf("You've entered 4\n"); break; case 5: printf("You've entered 5\n"); break; default: printf("You've entered something else\n"); exit(0); } return 0; } |
To find more switch statement examples in C programming, you must check these C programming examples.
Output

It is also possible to create nested switch statement in C programming. However, ANSI C permits a maximum of 15 levels of nested switch statements.
However, there’s a drawback of switch case labels. You cannot compare a value in the case expression such as the following which limits us for comparisons.
1 2 | case (num > 200): printf("Number is greater than 200"); break; |
If you have any compilation errors or doubts about switch statement in C programming, let us discuss in the comment section below. Find more on MSDN.