🌱 C Basics
if & else: teaching programs to choose
▶ Open the interactive lesson — free, no signupWrong password? Show an error. Score over 100? Fireworks. Every choice an app makes is an if — and in 2014, one misplaced line of if-code left millions of iPhones open to eavesdropping, a bug famous enough to have a name: "goto fail". Twenty minutes from now, you'll be able to spot it yourself.
So far your programs run straight down, line by line, same story every time. The if statement changes everything: code that only runs when a condition holds. This is the moment programs stop being calculators and start being deciders.
#include <stdio.h>
int main(void) {
int temp = 33;
if (temp > 30) {
printf("It's hot — hydrate!\n");
} else {
printf("Pleasant enough.\n");
}
printf("(this line runs either way)\n");
return 0;
}$ gcc weather.c -o weather && ./weather It's hot — hydrate! (this line runs either way)
The shape: if (condition) { … } else { … }. The condition is any expression — remember, nonzero means true. The else branch is optional and runs exactly when the condition was false. One path or the other, never both:
▶ This spot has an interactive flow widget — open the interactive lesson to play with it.
🧠 Checkpoint: When does the else branch run?
- Always, after the if branch
- Exactly when the condition evaluated to 0
- Only if the condition is negative
- When the if branch crashes
Show answer
Exactly when the condition evaluated to 0 — if/else is a fork: condition nonzero → if-branch, condition zero → else-branch. Never both, never neither.
else-if ladders: many-way choices
C has no special "elif" keyword — else if is literally an else whose statement is another if. Chain them and you get a ladder that finds the first true condition, runs that branch, and skips the rest:
#include <stdio.h>
int main(void) {
int score = 78;
if (score >= 90) {
printf("A — outstanding\n");
} else if (score >= 80) {
printf("B — great\n");
} else if (score >= 70) {
printf("C — solid\n");
} else if (score >= 60) {
printf("D — close call\n");
} else {
printf("F — see you in summer school\n");
}
return 0;
}$ gcc grade.c -o grade && ./grade C — solid
Order matters! The ladder tests top-down, so put the most specific (or most likely) conditions first. Once a branch fires, the remaining rungs aren't even evaluated.
🧠 Checkpoint: In the grade ladder, score = 95. Why doesn’t it also print "B — great"? After all, 95 ≥ 80…
- Because printf can only run once
- The ladder stops at the FIRST true condition; later rungs are skipped
- The compiler removes duplicate branches
- It does print both
Show answer
The ladder stops at the FIRST true condition; later rungs are skipped — An else-if ladder is one big fork: the first true condition claims execution and everything after the matching branch is skipped — the 80-check never even runs.
Braces: the cheap insurance
Technically, if controls just one statement, so braces are optional for a single line. Practically, skipping them is how famous bugs happen — Apple's 2014 "goto fail" SSL vulnerability was exactly a misindented extra line after an unbraced if. Look at this trap:
int coins = 0;
if (coins > 0)
printf("You can buy a snack!\n");
printf("Purchase complete.\n"); /* ALWAYS runs — not in the if! */The indentation lies. Only the first printf is inside the if — the second runs unconditionally. The compiler doesn't read indentation; it reads braces.
House rule for this course: always use braces, even for one-liners. It costs two characters and eliminates a whole species of bug (plus the "dangling else" below).
The dangling else
Quick: which if does this else belong to?
if (temp > 20)
if (sunny)
printf("beach day!\n");
else /* indented to match the OUTER if… */
printf("not sunny\n"); /* …but binds to the INNER one! */The rule: an else binds to the nearest unmatched if — here the inner one, despite the indentation suggesting otherwise. So "not sunny" prints only when temp > 20 but it isn't sunny, and cold days print nothing. Braces would have made the intent unambiguous — one more reason they're house rules.
🧠 Checkpoint: In the dangling-else example, temp = 10 (cold). What prints?
- "not sunny"
- Nothing at all
- "beach day!"
- Compile error
Show answer
Nothing at all — The else belongs to the inner if (sunny). When temp ≤ 20 the whole inner if/else is skipped, so nothing prints. Braces around the outer if-body would restore the intended meaning.
▶ This spot has an interactive editor widget — open the interactive lesson to play with it.
Ladders with many else if rungs comparing one variable against constants have a dedicated, cleaner tool — meet switch.