If-else vs Switch
Both can make decisions. if-else is more flexible for ranges and complex conditions. switch is convenient when one expression is matched against fixed values.
Syntax / Basic Pattern
| if (marks >= 80) … switch(day) { case 1: … } |
Easy points to remember
- Use if-else for ranges such as marks >= 80.
- Use switch for fixed choices such as menu 1, 2, 3.
- Do not choose a construct only because it is shorter; choose the one that matches the problem.
| ⭐ PRACTICE TIP: Type these programs yourself. Change the values and run them again. That is how control statements become easy. |
Program 1: Fixed menu: if-else
| #include <stdio.h> int main(){int c=2;if(c==1)printf(“Add”);else if(c==2)printf(“View”);else printf(“Exit”);return 0;} |
| OUTPUT View |
Program 2: Fixed menu: switch
| #include <stdio.h> int main(){int c=2;switch(c){case 1:printf(“Add”);break;case 2:printf(“View”);break;default:printf(“Exit”);}return 0;} |
| OUTPUT View |
Program 3: Range: if-else
| #include <stdio.h> int main(){int m=82;if(m>=75)printf(“A”);else if(m>=40)printf(“Pass”);else printf(“Fail”);return 0;} |
| OUTPUT A |
Program 4: Range cannot directly use switch
| #include <stdio.h> int main(){int m=82;switch(m/10){case 10:case 9:case 8:printf(“A”);break;case 7:printf(“B”);break;default:printf(“Other”);}return 0;} |
| OUTPUT A |
Program 5: Day: switch
| #include <stdio.h> int main(){int d=5;switch(d){case 5:printf(“Friday”);break;default:printf(“Other”);}return 0;} |
| OUTPUT Friday |
Program 6: Day: if-else
| #include <stdio.h> int main(){int d=5;if(d==5)printf(“Friday”);else printf(“Other”);return 0;} |
| OUTPUT Friday |
Program 7: Sign: if-else
| #include <stdio.h> int main(){int n=-4;if(n>0)printf(“Positive”);else if(n<0)printf(“Negative”);else printf(“Zero”);return 0;} |
| OUTPUT Negative |
Program 8: Operator: switch
| #include <stdio.h> int main(){char op=’*’;int a=4,b=5;switch(op){case ‘+’:printf(“%d”,a+b);break;case ‘*’:printf(“%d”,a*b);break;default:printf(“Invalid”);}return 0;} |
| OUTPUT 20 |
Program 9: Vowel: switch
| #include <stdio.h> int main(){char c=’i’;switch(c){case ‘a’:case ‘e’:case ‘i’:case ‘o’:case ‘u’:printf(“Vowel”);break;default:printf(“Consonant”);}return 0;} |
| OUTPUT Vowel |
Program 10: Vowel: if-else
| #include <stdio.h> int main(){char c=’i’;if(c==’a’||c==’e’||c==’i’||c==’o’||c==’u’)printf(“Vowel”);else printf(“Consonant”);return 0;} |
| OUTPUT Vowel |
Program 11: Traffic: switch
| #include <stdio.h> int main(){char c=’G’;switch(c){case ‘R’:printf(“Stop”);break;case ‘G’:printf(“Go”);break;default:printf(“Wait”);}return 0;} |
| OUTPUT Go |
Program 12: Age range: if-else
| #include <stdio.h> int main(){int age=35;if(age<18)printf(“Minor”);else if(age<60)printf(“Adult”);else printf(“Senior”);return 0;} |
| OUTPUT Adult |
Program 13: Month: switch
| #include <stdio.h> int main(){int m=11;switch(m){case 11:printf(“November”);break;default:printf(“Other”);}return 0;} |
| OUTPUT November |
Program 14: Exact comparison: if
| #include <stdio.h> int main(){int a=10,b=10;if(a==b)printf(“Equal”);else printf(“Not equal”);return 0;} |
| OUTPUT Equal |
Program 15: Fixed grade code: switch
| #include <stdio.h> int main(){char g=’C’;switch(g){case ‘A’:printf(“Excellent”);break;case ‘B’:printf(“Very good”);break;case ‘C’:printf(“Good”);break;default:printf(“Other”);}return 0;} |
| OUTPUT Good |