Switch-Case Statement :
Most of the beginners stuck at this point , they didn’t understand what actually switch-case is doing and where we should prefer switch case over if-else .
But don’t worry I will tell you everything you need to know about switch-case , let’s relax your mind .
First of all let’s see how switch case looks like : ( Pseudo Code )
switch(condition) {
case condition1 :
print(“condition1 statement”)
break
case condition2 :
print(“condition2 statement”)
break
default :
print(“default statement”)
}
so when the condition inside switch and case’s condition is same that case is going to be executed if no case condition meets then default case will be executed .
( Remember this is default only not default case or case default )
you can see these case are in linear .
case1 -> case2 -> default
so if you don’t use break then after case1, case2 and default will also get executed , so be aware of using break , sometime it is good to use break wisely like :
case ‘a’:
case ‘A’:
print(“Alphabet A”)
break;
so in this if switch condition is ‘a’ or ‘A’ it will print Alphabet A .
( Remember to use break wisely )
Now let’s see some example :
Example-1:
#include<stdio.h>
void main() {
int x = 1;
switch(x) {
case 1:
printf(“case1 executed.”);
break;
case 2:
printf(“case2 executed.”);
break;
default:
printf(“default case executed.”);
}
}
Output
case1 executed.
Explanation
since switch condition is 1 so case 1 will be executed and break statement will take it out .
Example-2:
#include<stdio.h>
void main() {
int x = 1;
switch(x) {
case 1:
printf(“case1 executed.”);
case 2:
printf(“case2 executed.”);
default:
printf(“default case executed.”);
}
}
Output
case1 executed.
case2 executed.
default case executed.
Explanation
since switch condition is 1 so case 1 will be executed but there is no break so it will run all cases .
Example-3:
#include<stdio.h>
void main() {
switch((1+1)/2+1) {
case 1:
printf(“case1 executed.”);
break;
case 2:
printf(“case2 executed.”);
break;
default:
printf(“default case executed.”);
}
}
Output
case2 executed.
Explanation
(1+1)/2+1 = 2/2+1 = 1 + 1 = 2
so switch condition is 2 so case 2 will be executed and break will take it out .
Example-4:
#include<stdio.h>
void main() {
switch(2) {
case 1:
printf(“case1 executed.”);
break;
case 1+1:
printf(“case1+1 executed.”);
break;
default:
printf(“default case executed.”);
break;
}
}
Output
case1+1 executed.
Explanation
case1+1 is same as case2 so for switch(2) case 1+1 will be executed.
Example-5:
#include<stdio.h>
void main() {
switch(2) {
case 1:
printf(“case1 executed.”);
break;
case 1+1:
printf(“case1+1 executed.”);
break;
case 2:
printf(“case2 executed.”);
break;
default:
printf(“default case executed.”);
break;
}
}
Output
Compilation Error
5_switch.c:10:3: error: duplicate case value
10 | case 2:
| ^~~~
5_switch.c:7:3: note: previously used here
7 | case 1+1:
| ^~~~
Explanation
Duplicate cases are not possible .
Since case1+1 and case2 are same so compiler will give you error .
I think I have covered all points by these 5 examples .
Thank you …
If you find the content of this website interesting and helpful, use the “Buy HW Programmer a Coffee” link below to buy me a coffee. Your generosity will keep me caffeinated and Inspire me to keep the website up and running .
Thank you so much for your consideration.
https://ko-fi.com/hwprogrammer
Let’s Connect with us :
Instagram : https://www.instagram.com/hwprogrammer/
Facebook Page : https://www.facebook.com/hwcoder
Facebook Group : https://www.facebook.com/groups/838100380386300