Introduction
An if statement runs its block only when its condition is true. In C, a condition is just a number: 0 means false and anything else means true.
Programs become useful when they can choose. Conditions in C are unusually simple, because they are only numbers, and that simplicity is the source of one of the language's best-known traps.
How it works
The = trap
if (x = 5) does not compare. It stores 5 in x, and since 5 is not 0 the block always runs. Real C accepts this silently; StepVioz stops and explains, because it is almost always a typo for ==.
switch
When you compare one value against many fixed choices, switch is clearer:
#include <stdio.h>
int main(void) {
int day = 6;
switch (day) {
case 1:
printf("Sunday: classes\n");
break;
case 6:
case 7:
printf("Weekend\n");
break;
default:
printf("A weekday\n");
}
return 0;
}Each case needs a break, or C carries on into the next case. That "fall through" is occasionally useful, as in case 6: case 7: sharing one result.
Syntax
if (x > 0) {
...
} else if (x == 0) {
...
} else {
...
}
switch (n) {
case 1:
...
break;
default:
...
}
Example
#include <stdio.h>
int main(void) {
int marks = 75;
char grade;
if (marks >= 80) {
grade = 'A';
} else if (marks >= 70) {
grade = 'B';
} else if (marks >= 60) {
grade = 'C';
} else {
grade = 'F';
}
printf("Marks %d: grade %c\n", marks, grade);
if (marks >= 40 && marks <= 100) printf("Passed\n");
return 0;
}C checks the conditions from the top. The first one that is true wins, and the rest are skipped. Step through and watch the calculation box: it shows each comparison with the real values, such as marks >= 80 → 75 >= 80 → false.
| Operator | Meaning |
|---|---|
== != | equal, not equal |
< <= > >= | comparisons |
&& | and: both must be true |
|| | or: at least one must be true |
! | not |
Line by line
marksis 75, andgradehas no value yet.marks >= 80is false, so C goes on to the next test.marks >= 70is true, sogradebecomes'B'and the rest of the chain is skipped.printfshows the marks and the grade.- The last
ifuses&&, and both of its sides are true.
Expected result
Marks 75: grade B
Passed
In the real world
A thermostat, a login check and a game's rules are all built from conditions. A misplaced = inside an if has caused real security bugs, which is why compilers offer a warning for it.
Common mistakes
=instead of==in a condition.- A
;right after the condition:if (x > 0);ends the if there, so the block below always runs. - Forgetting
breakin a switch. - Writing
if (0 < x < 10): C works out0 < xfirst (0 or 1), then compares that with 10. Writex > 0 && x < 10.
Best practices
- Always use braces, even for one line.
- Compile with warnings turned on, such as
-Wall. - Prefer
switchwhen comparing one value with many fixed choices.
Summary
if chains run the first true branch. Comparisons give 0 or 1, and &&, || and ! combine them. == compares and = assigns. switch picks among fixed cases, each ended by break.
Quick check
Key takeaways
- In C, 0 is false and anything else is true.
- Only the first true branch runs.
- Each case in a switch needs a break.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.