If Else in C
if-else is used when a program has to choose between two or more paths. The condition is checked first. If it is true, the if block runs; otherwise the else block runs.
Syntax / Basic Pattern
| if (condition) { // true block } else { // false block } |
Easy points to remember
- Use if-else for decisions such as pass/fail, even/odd, eligibility and comparisons.
- For more than two choices, use else-if.
- Use == for comparison; use = for assignment.
| ⭐ PRACTICE TIP: Type these programs yourself. Change the values and run them again. That is how control statements become easy. |
Program 1: Positive or negative
| #include <stdio.h> int main(){ int n=12; if(n>=0) printf(“Non-negative”); else printf(“Negative”); return 0;} |
| OUTPUT Non-negative |
Program 2: Even or odd
| #include <stdio.h> int main(){ int n=17; if(n%2==0) printf(“Even”); else printf(“Odd”); return 0;} |
| OUTPUT Odd |
Program 3: Pass or fail
| #include <stdio.h> int main(){ int marks=68; if(marks>=40) printf(“Pass”); else printf(“Fail”); return 0;} |
| OUTPUT Pass |
Program 4: Adult or minor
| #include <stdio.h> int main(){ int age=17; if(age>=18) printf(“Adult”); else printf(“Minor”); return 0;} |
| OUTPUT Minor |
Program 5: Greater of two
| #include <stdio.h> int main(){ int a=25,b=19; if(a>b) printf(“%d is greater”,a); else printf(“%d is greater”,b); return 0;} |
| OUTPUT 25 is greater |
Program 6: Divisible by 5
| #include <stdio.h> int main(){ int n=45; if(n%5==0) printf(“Divisible by 5”); else printf(“Not divisible by 5”); return 0;} |
| OUTPUT Divisible by 5 |
Program 7: Profit or loss
| #include <stdio.h> int main(){ float cp=500,sp=620; if(sp>cp) printf(“Profit = %.2f”,sp-cp); else printf(“No profit”,0.0); return 0;} |
| OUTPUT Profit = 120.00 |
Program 8: Eligible to vote
| #include <stdio.h> int main(){ int age=22; if(age>=18) printf(“Eligible to vote”); else printf(“Not eligible”); return 0;} |
| OUTPUT Eligible to vote |
Program 9: Largest of three
| #include <stdio.h> int main(){ int a=15,b=42,c=28; if(a>=b&&a>=c) printf(“Largest=%d”,a); else if(b>=a&&b>=c) printf(“Largest=%d”,b); else printf(“Largest=%d”,c); return 0;} |
| OUTPUT Largest=42 |
Program 10: Grade
| #include <stdio.h> int main(){ int m=76; if(m>=90) printf(“A+”); else if(m>=75) printf(“A”); else if(m>=60) printf(“B”); else if(m>=40) printf(“C”); else printf(“F”); return 0;} |
| OUTPUT A |
Program 11: Leap year
| #include <stdio.h> int main(){ int y=2024; if((y%400==0)||(y%4==0&&y%100!=0)) printf(“Leap year”); else printf(“Not leap year”); return 0;} |
| OUTPUT Leap year |
Program 12: Number in range
| #include <stdio.h> int main(){ int n=35; if(n>=1&&n<=50) printf(“Inside range”); else printf(“Outside range”); return 0;} |
| OUTPUT Inside range |
Program 13: Character vowel
| #include <stdio.h> int main(){ char ch=’e’; if(ch==’a’||ch==’e’||ch==’i’||ch==’o’||ch==’u’) printf(“Vowel”); else printf(“Consonant”); return 0;} |
| OUTPUT Vowel |
Program 14: Temperature check
| #include <stdio.h> int main(){ float t=38.5; if(t>=40) printf(“Very hot”); else if(t>=30) printf(“Hot”); else printf(“Moderate”); return 0;} |
| OUTPUT Hot |
Program 15: Login check
| #include <stdio.h> #include <string.h> int main(){ char user[]=”admin”, pass[]=”1234″; if(strcmp(user,”admin”)==0&&strcmp(pass,”1234″)==0) printf(“Login successful”); else printf(“Invalid login”); return 0;} |
| OUTPUT Login successful |