Another alternative to a multipath decision is the switch statement. For many alternatives the else if ladder may be a bit awkward to use. This switch case is like a special use case of an else-if ladder which works only for an integer number for the matching case.
Following is the syntax for the switch case:
switch (test_expression){
case const1:
// matching block 1
break;
case const2:
// matching block 2
break;
case const3:
// matching block 3
break;
case const4:
// matching block 4
break;
case constn:
// matching block n
break;
default:
// default match block
break;
}
In switch construct, test_expression is evaluated first and result that matches with the one case constant is executed. Similar to else if ladder switch construct has optional default block. If default block is present and no any cases above matches then the default block is executed. The break keyword is optional, and if not present the execution of program continues starting from matching block until the break statement is occured.
The test_expression must result to an integer or character value. const1, const2, const3, const4,…. constn are constant expression evaluable to an integral constant known as case labels. And these values must be unique within a switch statement.
The block diagram for switch case is shown below.

Switch case has certain rules as listed below.
- The switch test_expression must result/be an integral type.
- Case labels must be constants or constant expression.
- Case labels must be unique. No two labels can have same values.
- Case labels must end with colon.
- The break statement transfers the control out of the switch statement.
- break keyword is optional. That means two or more case labels may belong to the same block.
- The default label is optional. If not present no statements will be executed in case of no matched block.
- There can be no more than one default block.
- The default can be placed anywhere within the switch block, but generally placed at the end.
- The switch statements can be nested.
Let’s see the simple example scenario solved by else if ladder by using switch case.
// switch case example
#include <stdio.h>
int main(){
int day;
printf("Enter an integer number:");
scanf("%d", &day);
switch (day){
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Number does not match.");
break;
}
return 0;
}
Sample Output:
Enter an integer number:34
Number does not match.