Quick links
if...else statement provides support to control program flow. if statement make decisions based on conditions. It selects an action, if some condition is met. However, there exits situations where you want to make a decision from available choices. For example – select a laptop from available models, select a menu from available menu list etc.
switch...case statement gives ability to make decisions from fixed available choices. Rather making decision based on conditions. Using switch we can write a more clean and optimal code, that take decisions from available choices.
Syntax of switch...case statement
switch(expression)
{
case 1:
/* Statement/s */
break;
case 2:
/* Statement/s */
break;
case n:
/* Statement/s */
break;
default:
/* Statement/s */
}Rules for working with switch case
- Expression inside
switchmust evaluate to integer, character or enumeration constant.switch...caseonly works with integral, character or enumeration constant. - The
casekeyword must follow one constant of type evaluated by expression. Thecasealong with a constant value is known as switch label. - You can have any number of cases.
- Each and every
casemust be distinct from other. For example, it is illegal to write twocase 1label. - You are free to put cases in any order. However, it is recommended to put them in ascending order. It increases program readability.
- You can have any number of statement for a specific case.
- The
breakstatement is optional. It transfers program flow outside ofswitch...case.breakstatement is covered separately in this C tutorial series.Read more about – break statement in C.
- The
defaultcase is optional. It works like anelseblock. If no cases are matched then the control is transferred todefaultblock.
Working of switch...case statement
Let me take an example to demonstrate the working of switch...case statement.
int num = 2;
switch(num)
{
case 1: printf("I am One");
break;
case 2: printf("I am Two");
break;
case 3: printf("I am Three");
break;
default: printf("I am an integer. But, definitely I am not 1, 2 and 3.");
}- Initially I declared an integer variable
num = 2. switch(num)will evaluate the value of num to 2.- After
switch(num)got evaluated,switchknows thecaseto transfer program control. - Instead of checking all cases one by one. It transfers program control directly to
case 2. If value of num is not matched with anycasethenswitchtransfers control todefaultcase, if defined. - After the control has been set to
case 2. It executes all statements inside the case. The case contains two statement firstprintf("I am Two");and secondbreak.printf("I am Two");will print “I am Two” on console and transfers control tobreak. breakstatement terminatesswitch...caseand transfer program control to statement afterswitch.What if I don’t use
breakkeyword? If you don’t usebreakkeyword, it executes all below cases untilbreakstatement is found. It doesn’t matter whether the remaining case matches or not, it will execute all below case from matching case in the absence ofbreakkeyword.
Flowchart of switch...case statement

Example program of switch...case statement
Let us write a C program to input week number from user and print the corresponding day name of week.
/**
* C program to print day of week name
*/
#include <stdio.h>
int main()
{
/* Declare integer variable to store week number */
int week;
/* Input week number from user */
printf("Enter week number (1-7): ");
scanf("%d", &week);
switch(week)
{
case 1:
/* If week == 1 */
printf("Its Monday.\n");
printf("Its a busy day.");
break;
case 2:
/* If week == 2 */
printf("Its Tuesday.");
break;
case 3:
/* If week == 3 */
printf("Its Wednesday.");
break;
case 4:
/* If week == 4 */
printf("Its Thursday.\n");
printf("Feeling bit relaxed.");
break;
case 5:
/* If week == 5 */
printf("Its Friday.");
break;
case 6:
/* If week == 6 */
printf("Its Saturday.\n");
printf("It is weekend.");
break;
case 7:
/* If week == 7 */
printf("Its Sunday.\n");
printf("Hurray! Its holiday.");
break;
default:
/* If week < 1 or week > 7 */
printf("Um! Please enter week number between 1-7.");
}
return 0;
}Practice exercises – Switch case programming exercises in C.
Output –
Enter week number (1-7): 6
Its Saturday.
It is weekend.Nesting of switch...case statement
C supports nesting of one switch...case inside other. However, it is not recommended model to put one switch...case inside other. As nesting of switch decreases code readability.
Syntax of nested switch...case statement
switch(expression)
{
case 1:
/* Statement/s */
break;
case 2:
/* Statement/s */
switch(inner_expression)
{
case 1:
/* Statement/s */
break;
case 2:
/* Statement/s */
break;
case n:
/* Statement/s */
break;
default:
/* Statement/s */
break;
}
break;
case n:
/* Statement/s */
break;
default: /* Default statement/s */
}