C If..Else
Programs become interesting when they make decisions. The if statement runs a block of code only when a condition is true. Together with else and else if, it lets your program choose between several paths.
if
if (condition) {
// runs only when condition is true (non-zero)
}#include <stdio.h>
int main(void) {
int temperature = 31;
if (temperature > 30) {
printf("It's hot today. Drink water!\n");
}
printf("Weather check done.\n");
return 0;
}It's hot today. Drink water!
Weather check done.
Change 31 to 20 and run again: the first message disappears, but the last line always prints because it is outside the if block.
else
else gives an alternative block that runs when the condition is false. Exactly one of the two blocks runs.
#include <stdio.h>
int main(void) {
int number = 7;
if (number % 2 == 0) {
printf("%d is even\n", number);
} else {
printf("%d is odd\n", number);
}
return 0;
}7 is odd
else if
To choose between more than two options, chain conditions with else if. They are tested top to bottom, and only the first true one runs. The final else catches everything left over.
#include <stdio.h>
int main(void) {
int score = 78;
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
printf("Score %d gives grade %c\n", score, grade);
return 0;
}Score 78 gives grade C
Because the checks run in order, the score >= 70 branch does not need to also say score < 80 — any score of 80 or more was already caught above.
Combining conditions
Use the logical operators && (and), || (or) and ! (not) to build richer conditions:
#include <stdio.h>
int main(void) {
int hour = 14;
int is_weekend = 0;
if (is_weekend || hour < 9 || hour >= 17) {
printf("The shop is closed.\n");
} else if (hour >= 12 && hour < 13) {
printf("Lunch break, back soon.\n");
} else {
printf("The shop is open.\n");
}
return 0;
}The shop is open.
Nested if
An if can contain another if. Keep nesting shallow — two or three levels at most — or the code becomes hard to follow.
#include <stdio.h>
int main(void) {
int age = 20;
int has_license = 1;
if (age >= 18) {
if (has_license) {
printf("You may drive.\n");
} else {
printf("Get a license first.\n");
}
} else {
printf("Too young to drive.\n");
}
return 0;
}You may drive.
Short form: the ternary operator
For choosing between two values, the conditional operator ?: is a compact alternative:
int max = (a > b) ? a : b;
printf("%s\n", logged_in ? "Welcome back" : "Please log in");Common mistakes
| Mistake | Problem | Fix |
|---|---|---|
if (x = 5) |
assigns 5, condition is always true | if (x == 5) |
if (x > 0); |
the ; is an empty statement; the block below always runs |
remove the semicolon |
if (0 < x < 10) |
evaluates as (0 < x) < 10, which is always true |
if (x > 0 && x < 10) |
| missing braces | only the first statement belongs to the if |
always use { } |
The last point deserves an example. Without braces, only one statement is controlled:
if (balance < 0)
printf("Overdrawn!\n");
charge_fee(); // runs ALWAYS — the indentation liesTip: Always write braces, even around a single statement. It prevents this bug when someone later adds a second line.
Test yourself
With int x = 15;, what does if (x > 10) printf("A"); else if (x > 5) printf("B"); else printf("C"); print?
- A
- B
- AB
- C
Why is if (count = 0) almost always a bug?
- It is a syntax error
- It assigns 0 to count and the condition is always false
- It compares count with 0
- It crashes the program
How do you correctly test that x lies between 1 and 9 (exclusive)?
- if (1 < x < 9)
- if (x > 1 && x < 9)
- if (x > 1 || x < 9)
- if (1 < x, x < 9)