
C Programming Switch Case Statement
Table of Contents
By the end of this chapter, you will learn how to write switch statements in C programming.
C programming switch statement
By using nested if…else statements you can execute a specific block of code against many values. But if you want to check the value of single variable then it is better to use nested…else statement instead of nested if…else statement. switch
switch statement are often if…else faster than nested statements and the syntax of switch statements is relatively simple and transparent.
C Switch Syntax
switch (expression)
{
case value1:
//This code will be executed if the value of expression is equal to value1;
break;
case value2:
//This code will be executed if the value of expression is equal to value2;
break;
.
.
.
default:
//This code will be executed if the value of the expression is not equal to any value;
}
How does the switch...case statement work?
- The expression in the above syntax is executed only once in the program.
- The value of the expression corresponds to the case value of the switch blockl the program control is passed to it, so the code block of that case value is executed.
- If the value of expression in the above pseudocode is equal to value 2 then the compiler will execute the code block of case value 2.
- If there is no beak statement after case value2, the switch block will execute until the end.
- If the value of the expression does not match any of the case value of the switch block, the code block of default will be executed.
- The break statement is used to prevent the next case from executing.
Example 1: Switch Statement
#include
#include
int main()
{
int num;
clrscr();
printf("Enter any number (1 to 7) \n: ");
scanf("%d",&num);
switch(num)
{
case 1:
printf("Today is Saturday");
break;
case 2:
printf("Today is Sunday");
break;
case 3:
printf("Today is Monday");
break;
case 4:
printf("Today is Tuesday");
break;
case 5:
printf("Today is Wednesday");
break;
case 6:
printf("Today is Thursday");
break;
case 7:
printf("Today is Friday");
break;
default:
printf("Only enter value 1 to 7");
}
getch();
}
output
Enter any number (1 to 7): 4
Today is Tuesday

In the above program user input 4, the value of the expression matches case 4 : and Tuesday is printed.
Example 2: Switch Statement
// Program for creating simple calculators
// Add, subtract, multiply, or divide based on user input.
# include
int main() {
char operator;
double firstNumber, secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;
This statement will be executed if the // operator does not match any case values (+, -, *, /).
default:
printf("Error! operator is not correct");
}
return 0;
}
output
Enter an operator (+, -, *,): -
Enter two operands: 50.27
20.25
32.5 - 12.4 = 30.02
User – operator input is stored in the operator variable. And given the two operands 32.5 and 12.4 as inputs, they are stored in the firstNumber and secondNumber variable respectively.
Then program control jumps to the following block.
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-firstNumber);
Finally the break statement terminates the switch statement.
If the break statement was not used, all case after the correct case would be executed.