Introduction
Everything so far has run every line, every time, in order. if is where a sketch starts to choose.
if (something is true) {
do this
}
The condition in the brackets is a yes-or-no question — exactly the kind you built in the boolean lesson. If the answer is yes, the block runs. If not, it is skipped entirely.
else catches everything the if did not. else if lets you test a second question, and a third, until one fits. Only one branch of the whole chain ever runs: the first one whose condition is true. That is the part people get wrong, and it is the part that makes a chain of else if different from several separate ifs.
The traps are small and famous. = where you meant ==. A stray semicolon after the closing bracket. And a missing else on the last branch, so a case you did not think about quietly does nothing at all.
What you will be able to do
By the end of this lesson you can:
- Run a block only when a condition is true.
- Add
elseto catch everything else. - Chain conditions with
else if, and say why order matters. - Explain why a chain of
else ifdiffers from several separateifs. - Spot
if (x = 5)andif (x == 5);as mistakes.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
→ if / else — the same ground with an LED and a button.
if
int temperature = 31;
if (temperature > 30) {
Serial.println("Hot");
}
The block runs only when the condition is true. Otherwise the sketch carries straight on.
Note what is not there: no semicolon after if (...), and none after the closing brace.
else
if (temperature > 30) {
Serial.println("Hot");
} else {
Serial.println("Not hot");
}
Exactly one of the two runs. Always. else needs no condition — it is "in every other case".
else if
if (temperature > 35) {
Serial.println("Very hot");
} else if (temperature > 30) {
Serial.println("Hot");
} else if (temperature > 25) {
Serial.println("Warm");
} else {
Serial.println("Cool");
}
Tested top to bottom. The first true one wins and the rest are skipped.
With temperature = 31: the first is false, the second is true, so it prints and never looks at the third.
Challenges
Challenge 1
Sort the temperature.
Set int temperature by hand and print one word for it:
| Reading | Word |
|---|---|
| above 35 | Very hot |
| 31 to 35 | Hot |
| 26 to 30 | Warm |
| 26 and below | Cool |
Test with 40, 33, 28, 20 — and then with exactly 30, 31 and 35. The boundaries are where mistakes hide.
Then deliberately reverse the order of your branches and note what breaks.
Log in to ask for the answer.
Challenge 2
Chain or separate?
Set int n = 20; and run both of these:
// version A
if (n > 10) { Serial.println("big"); }
else if (n > 5) { Serial.println("medium"); }
// version B
if (n > 10) { Serial.println("big"); }
if (n > 5) { Serial.println("medium"); }
Write down what each prints, and one sentence on when you would genuinely want version B.
Then try both with n = 7 and with n = 3.
Log in to ask for the answer.
Challenge 3
Extra challenge
A dice game.
Roll a dice every two seconds and react:
- a 6 prints
Six! Roll again - a 1 prints
Snake eyes - an even number prints
Even: n - anything else prints
Odd: n
Only one message per roll. Think carefully about the order — 6 is also even, and 1 is also odd.
Keep a running score too: add the roll to a total, except that a 1 resets the total to zero. Print the total each time.
Think about it: your chain has four branches. If the game grew to twenty different reactions, would twenty else if branches still be readable? That is the question switch answers in the next lesson but one.
Log in to ask for the answer.