if and if-else
Choosing a pathUntil now every statement ran, one after another. An if statement lets the program decide: run something only when a condition is true — and with else, choose between two paths. In the lab below you trace the path the program really takes: which lines run, which are skipped, and three bugs that compile without errors.
- ifruns a statement only when true
- elseruns when the condition is false
- { }group several statements
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
The shape of an if statement
if is followed by a condition in parentheses and then ONE statement — or a block of statements in { }. The condition is worked out; if it is true (non-zero) the statement runs, otherwise it is skipped. With else, the second statement runs when the condition is false. Afterwards the program continues with the next line.
if (condition)
statement; // runs only if true
if (condition) {
statement1; // a block: several statements
statement2;
} else {
statement3; // runs only if false
}
| Form | Example | What runs |
|---|---|---|
| if | if (marks >= 40) printf("Pass"); | the statement, only when the condition is true |
| if with a block | if (x < 0) { x = -x; count++; } | all statements in { }, only when true |
| if-else | if (n % 2 == 0) puts("even"); else puts("odd"); | exactly one of the two |
| if-else with blocks | if (a > b) { … } else { … } | exactly one of the two blocks |
| conditional operator | max = a > b ? a : b; | a short if-else that gives a value |
if
- The condition goes in parentheses: if (marks >= 40).
- Any expression can be a condition: 0 is false, anything else is true.
- Without braces, the if controls only the ONE next statement.
- With braces { }, it controls the whole block.
- After the if, the program continues with the next statement either way.
if-else
- else means "otherwise": it runs when the condition is false.
- Exactly one of the two branches runs — never both, never neither.
- else belongs to the if just before it; it cannot stand alone.
- Both branches can be single statements or blocks.
- For "choose a value" the conditional operator ?: is a short alternative.
Good habits
- Always use braces, even for one statement — it prevents the "missing braces" bug.
- Never put a ; right after if ( … ).
- Compare with ==, not =. Compile with -Wall -Wextra.
- Indent the branches so the structure is visible (C itself ignores indentation).
- Write ranges with &&: if (age >= 13 && age <= 19).
Three everyday decisions
Every line, explained.
#include <stdio.h>
int main(void)
{
int temp = 31;
if (temp > 30) {
31 > 30 is true, so the whole block runs.
printf("Hot day\n");
Prints Hot day
printf("Drink water!\n");
Prints Drink water! — also inside the block.
}
int n = -8;
if (n < 0)
A negative number…
n = -n;
…is made positive: the absolute value, 8.
int a = 12, b = 19, max;
if (a > b)
12 > 19 is false…
max = a;
…so this is skipped…
else
max = b;
…and the else runs: max is 19.
printf("%d %d\n", n, max);
Prints 8 19
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
A semicolon after the condition
if (x > 5);
printf("big");
if (x > 5)
printf("big");
The ; is an empty statement that becomes the body of the if. printf then always runs.
Missing braces
if (x > 5)
printf("big");
x = 0;
if (x > 5) {
printf("big");
x = 0;
}
Without braces only the first statement belongs to the if; x = 0 always runs, whatever the indentation says.
= instead of ==
if (x = 0)
if (x == 0)
x = 0 sets x to 0 and is always false. gcc -Wall warns: suggest parentheses around assignment used as truth value.
A semicolon before else
if (x > 0) {
puts("positive");
};
else {
puts("not positive");
}
if (x > 0) {
puts("positive");
} else {
puts("not positive");
}
The ; after } ends the if statement, so the else has no if: "error: 'else' without a previous 'if'".
A range with || instead of &&
if (age >= 13 || age <= 19) // always true
if (age >= 13 && age <= 19)
Every age is either ≥ 13 or ≤ 19, so || is always true. Both conditions must hold: &&.
Check your understanding
Answer each question — you will see the explanation straight away.
int x = 4; if (x > 5) printf("A"); printf("B"); — what is printed?
The condition is false, so printf("A") is skipped; printf("B") is not part of the if.
Which runs when the condition of an if-else is false?
else is the "otherwise" branch.
Is if (5) true or false?
Only 0 is false.
Without braces, how many statements does an if control?
Only the next statement. Use { } for more.
What does if (n = 5) printf("five"); do when n is 2?
n = 5 assigns 5, which is true, so it prints — and n is changed.
if and if-else quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.