Switch Statement in C
C switch statement is a conditional statement that allows you to execute different code blocks based on the value of a variable or an expression. It is often used in place of if-else ladder when there are multiple conditions.
Example:
#include <stdio.h>
int main() {
// Switch variable
int var = 1;
// Switch statement
switch (var) {
case 1:
printf("Case 1 is Matched.");
break;
case 2:
printf("Case 2 is Matched.");
break;
case 3:
printf("Case 3 is Matched.");
break;
default:
printf("Default case is Matched.");
break;
}
return 0;
}
Output
Case 1 is Matched.
Explanation: In this program, the switch statement evaluates the variable var. Since var is 1, the code in case with value 1 executes, printing "Case 1 is Matched." and then exiting the switch block due to the break statement. Other cases and the default case are skipped.
Syntax of switch
switch (expression) {
case value1:
// Code_block1
break;
case value2:
// Code_block2
break;
default:
// Default_code_block
}
Here,
- expression: Expression or variable whose value is tested. It should result in an integer or character value.
- case: Each switch case represents a block of code that will be executed with the expression evaluates to its corresponding value. There should be atleast one case in the switch.
- value1, value2: These are the possible values of the expression for which the corresponding cases are executed. These values must be unique.
- break: This is used to exit the switch statement once a case is executed. Without break, the program will continue executing the subsequent cases (this is called "fall through").
- default: This block of code is executed if none of the case values match the expression. It is optional to add this block.
Flowchart of C switch Statement

Working of Switch Statement
The working of the switch statement in C is as follows:
- Step 1: The switch variable is evaluated
- Step 2: The evaluated value is matched against all the present cases.
- Step 3A: If the matching case value is found, the associated code is executed.
- Step 3B: If the matching code is not found, then the default case is executed if present.
- Step 4A: If the break keyword is present in the executed case, then program control breaks out of the switch statement after executing the code block associated with the first matching condition.
- Step 4B: If the break keyword is not present, then all the cases after the matching case are executed
- Step 5: Statements after the switch statement are executed.
Examples of Switch Statement
The below programs show some use cases of switch statement in practical scenario:
Print Day Name of the Week
#include <stdio.h>
int main() {
// Day number
int day = 2;
// Switch statement to print the name of the day
// on the basis of day number
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid Input");
break;
}
return 0;
}
Output
Tuesday
Simple Calculator using Switch Statement
#include <stdio.h>
#include <stdlib.h>
int main() {
// two number
int x, y;
// Variable that select operation to perform
// i.e. switch variable
char choice;
// Take input
printf("Enter the Operator (+,-,*,/)\n");
scanf(" %c", &choice);
printf("Enter the two numbers: ");
scanf("%d %d", &x, &y);
// switch case with operation for each operator
switch (choice) {
case '+':
printf("%d + %d = %d\n", x, y, x + y);
break;
case '-':
printf("%d - %d = %d\n", x, y, x - y);
break;
case '*':
printf("%d * %d = %d\n", x, y, x * y);
break;
case '/':
printf("%d / %d = %d\n", x, y, x / y);
break;
default:
printf("Invalid Operator Input\n");
}
return 0;
}
Output
Enter the Operator (+,-,*,/)
+
Enter the two numbers: 10 20
10 + 20 = 30
C switch Statement without Break
The break keyword is used to stop the execution inside a switch block. It helps to terminate the switch block and break out of it. If omitted, execution will continue on into the next case.
#include <stdio.h>
int main() {
int var = 2;
// switch case without break
switch (var) {
case 1:
printf("Case 1 is executed.\n");
case 2:
printf("Case 2 is executed.\n");
case 3:
printf("Case 3 is executed.\n");
case 4:
printf("Case 4 is executed.");
}
return 0;
}
Output
Case 2 is executed. Case 3 is executed. Case 4 is executed.
Nested switch Statement
Nesting of switch statements is allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes the program more complex and less readable.
#include <stdio.h>
int main() {
int outerChoice = 1;
int innerChoice = 2;
// Outer switch to choose the category
switch(outerChoice) {
case 1:
printf("Category 1 selected\n");
// Inner switch to choose the option under category 1
switch(innerChoice) {
case 1:
printf("Option 1 selected under Category 1\n");
break;
case 2:
printf("Option 2 selected under Category 1\n");
break;
default:
printf("Invalid option under Category 1\n");
}
break;
case 2:
printf("Category 2 selected\n");
// Inner switch to choose the option under category 2
switch(innerChoice) {
case 1:
printf("Option 1 selected under Category 2\n");
break;
case 2:
printf("Option 2 selected under Category 2\n");
break;
default:
printf("Invalid option under Category 2\n");
}
break;
default:
printf("Invalid category\n");
}
return 0;
}
Output
Category 1 selected Option 2 selected under Category 1