The switch case statement is used when we have multiple options and we need to perform a different task for each option.
C – Switch Case Statement
Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.
switch (variable or an integer expression) { case constant: //C Statements ; case constant: //C Statements ; default: //C Statements ; }
Flow Diagram of Switch Case
Example of Switch Case in C
Let’s take a simple example to understand the working of a switch case statement in C program.
#include <stdio.h> int main() { int num=2; switch(num+2) { case 1: printf("Case1: Value is: %d", num); case 2: printf("Case1: Value is: %d", num); case 3: printf("Case1: Value is: %d", num); default: printf("Default: Value is: %d", num); } return 0; }
Output:
Default: value is: 2
Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where num value is 2 and after addition the expression resulted 4. Since there is no case defined with value 4 the default case is executed.
Twist in a story – Introducing Break statement
Before we discuss more about break statement, guess the output of this C program.
#include <stdio.h> int main() { int i=2; switch (i) { case 1: printf("Case1 "); case 2: printf("Case2 "); case 3: printf("Case3 "); case 4: printf("Case4 "); default: printf("Default "); } return 0; }
Output:
Case2 Case3 Case4 Default
I passed a variable to switch, the value of the variable is 2 so the control jumped to the case 2, However there are no such statements in the above program which could break the flow after the execution of case 2. That’s the reason after case 2, all the subsequent cases and default statements got executed.
How to avoid this situation?
We can use break statement to break the flow of control after every case block.
Break statement in Switch Case
Break statements are useful when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the control comes out of the switch case statement.
Example of Switch Case with break
I’m taking the same above that we have seen above but this time we are using break.
#include <stdio.h> int main() { int i=2; switch (i) { case 1: printf("Case1 "); break; case 2: printf("Case2 "); break; case 3: printf("Case3 "); break; case 4: printf("Case4 "); break; default: printf("Default "); } return 0; }
Output:
Case 2
Why didn’t I use break statement after default?
The control would itself come out of the switch after default so I didn’t use it, however if you want to use the break after default then you can use it, there is no harm in doing that.
Few Important points regarding Switch Case
1) Case doesn’t always need to have order 1, 2, 3 and so on. They can have any integer value after case keyword. Also, case doesn’t need to be in an ascending order always, you can specify them in any order as per the need of the program.
2) You can also use characters in switch case. for example –
#include <stdio.h> int main() { char ch='b'; switch (ch) { case 'd': printf("CaseD "); break; case 'b': printf("CaseB"); break; case 'c': printf("CaseC"); break; case 'z': printf("CaseZ "); break; default: printf("Default "); } return 0; }
Output:
CaseB
3) The expression provided in the switch should result in a constant value otherwise it would not be valid.
For example:
Valid expressions for switch –
switch(1+2+23) switch(1*2+3%4)
Invalid switch expressions –
switch(ab+cd) switch(a+b+c)
4) Nesting of switch statements are allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes program more complex and less readable.
5) Duplicate case values are not allowed. For example, the following program is incorrect:
This program is wrong because we have two case ‘A’ here which is wrong as we cannot have duplicate case values.
#include <stdio.h> int main() { char ch='B'; switch (ch) { case 'A': printf("CaseA"); break; case 'A': printf("CaseA"); break; case 'B': printf("CaseB"); break; case 'C': printf("CaseC "); break; default: printf("Default "); } return 0; }
6) The default statement is optional, if you don’t have a default in the program, it would run just fine without any issues. However it is a good practice to have a default statement so that the default executes if no case is matched. This is especially useful when we are taking input from user for the case choices, since user can sometime enter wrong value, we can remind the user with a proper error message that we can set in the default statement.
judith cherono says
write a c program using switch case statement to output the following
2*3=6
2+3=5
4+6=10
4/5=0.8
RizanaParvin says
#include <stdio.h>
void main()
{
char operator;
int num1,num2;
printf(“\n Enter the operator (+, -, *, /):”);
scanf(“%c”,&operator);
printf(“\n Enter the Two numbers:”);
scanf(“%d%d”,&num1,&num2);
switch (operator)
{
case ‘+’:
printf(“%d+%d=%d”,num1,num2,num1+num2);
break;
case ‘-‘:
printf(“%d-%d=%d”,num1,num2,num1-num2);
break;
case ‘*’:
printf(“%d*%d=%d”,num1,num2,num1*num2);
break;
case ‘/’:
printf(“%d / %d = %d”,num1,num2,num1/num2);
break;
default:
printf(“\n Enter the operator only”);
break;
}
}
Muhammad Aliyan says
Thanks for your answer!
Nana Percy says
Please can I get the soluton to this question?
teboho says
wow that’s a beautiful explanation
vikash tiwari says
wow! how smoothly you explain it , it’s great.
Syed Minhaj Hussain says
Using Switch statement, write a program that displays the following menu for the food items available to take order from the customer:
• B= Burger
• F= French Fries
• P= Pizza
• S= Sandwiches
The program inputs the type of food and quantity. It finally displays the total charges for the order according to following criteria:
• Burger = Rs. 200
• French Fries= Rs. 50
• Pizza= Rs. 500
• Sandwiches= Rs. 150
FARZANA says
#include <stdio.h>
int main()
{
printf(“B=BURGER\nF=FRENCH FRY\nP=PIZZA\nS=SANDWICHES\n”);
char ss;
int n,x;
scanf(“%c”,&ss);
switch(ss)
{
case ‘B’:
scanf(“%d”,&n);
x= n*200;
printf(“Burger=Rs %d”,x);
case ‘F’:
scanf(“%d”,&n);
x= n*50;
printf(“Burger=Rs %d”,x);
case ‘P’:
scanf(“%d”,&n);
x= n*500;
printf(“Burger=Rs %d”,x);
case ‘S’:
scanf(“%d”,&n);
x= n*150;
printf(“Burger=Rs %d”,x);
}
}
Nandlal Dnyaneshwar Patil says
Answer to the question asked by Syed Minhaj Hussain:
#include <stdio.h>
int main()
{
int b,f,p,s,Burger,French,Pizza,Sandwiches;
char ch,B,F,P,S;
printf(“b1=Burger\n2=French Fries\n3=pizza\n4=Sandwiches\n”);
printf(“Enter your order \nplease Enter the choice 1,2,3,4\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“your order is Burger\n”);
printf(“please enter your quantity “);
scanf(“%d”,&b);
Burger=200*b;
printf(“your total charges is: %d”,Burger);
break;
case 2:
printf(“your order is French \n”);
printf(“please enter your quantity “);
scanf(“%d”,&f);
French=50*f;
printf(“your total charges is: %d”,French);
break;
case 3:
printf(“your order is Pizza\n”);
printf(“please enter your quantity “);
scanf(“%d”,&p);
Pizza=500*p;
printf(“your total charges is: %d”,Pizza);
break;
case 4:
printf(“your order is Sandwiches\n”);
printf(“please enter your quantity “);
scanf(“%d”,&s);
Sandwiches=150*s;
printf(“your total charges is: %d”,Sandwiches);
break;
default:
printf(“invalid your choice”);
break;
}
}
/*D:\cp>a/.out
b1=Burger
2=French Fries
3=pizza
4=Sandwiches
Enter your order
please Enter the choice 1,2,3,4
4
your order is Sandwiches
please enter your quantity 6
your total charges is: 900
D:\cp>a/.out
b1=Burger
2=French Fries
3=pizza
4=Sandwiches
Enter your order
please Enter the choice 1,2,3,4
6
invalid your choice*/
dhruv says
can we write an expression in case
like
case 1:
so in case of 1 can we write an expression?
Chaitanya Singh says
Yes we can, see the point no 3 above in the important notes section. Expressions are allowed in case.
morizonrobert says
Yes but only if you want to output the expression….
Padmanavo says
Can anyone please show how to use switch case system in a user defined function ?