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
case value 1: statement;
case value 2: statement;
case value 3: statement;
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
printf("\nEnter a Number below 5:\t");
case 1: printf("You've entered 1\n");
case 2: printf("You've entered 2\n");
case 3: printf("You've entered 3\n");
case 4: printf("You've entered 4\n");
case 5: printf("You've entered 5\n");
default: printf("You've entered something else\n");
To find more switch statement examples in C programming, you must check these C programming examples.
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.
case (num > 200): printf("Number is greater than 200");
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.