IF this, THEN that, ELSE the other
One sentence beginning with 'if' stops a robot vacuum falling down the stairs. What is that sentence?
About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator
1.Why should I care?
Conditions are where a program starts to respond to the world. Every intelligent machine you've met this term is built from rules like 'if the floor disappears, stop'. Learning to write them clearly is learning to give a machine good judgement.
- ◇ Read and write IF–THEN–ELSE rules that use a sensor reading.
- ◇ Decide which branch runs for a given reading.
- ◇ Explain why every rule needs an ELSE (or a deliberate 'do nothing').
2.Predict
Rule: IF distance < 20 cm THEN stop ELSE drive. The sensor reads exactly 20 cm. What does the robot do?
Guess first. Being wrong here is how the surprise works.
3.Discover
Set a rule, then slide the reading across the threshold. Look at the colour strip: every reading runs exactly one branch. Try switching < to > and see the strip flip.
If / else rule tester
A robot has a distance sensor on its nose. Write its rule, then move the reading and watch which branch runs. Try readings exactly on the threshold.
IF distance < 20 cm THEN Stop ELSE Drive forward
Is 35 < 20? No, so the ELSE branch runs: Drive forward.
Every reading from 0 to 100 cm (lime = THEN, cobalt = ELSE):
| Reading | Condition | Action |
|---|---|---|
| 5 cm | true | Stop |
| 19 cm | true | Stop |
| 20 cm | false | Drive forward |
| 21 cm | false | Drive forward |
| 60 cm | false | Drive forward |
| 100 cm | false | Drive forward |
Can't use the interactive? Read a description instead
The rule tester can be replaced by this table for the rule IF distance < 20 cm THEN stop ELSE drive: 5 cm → stop; 19 cm → stop; 20 cm → drive; 21 cm → drive; 60 cm → drive; 100 cm → drive.
4.Watch
→ Watch for the if-statement in the game example: a condition that is true or false, and code that runs only in one case.
Walks through if-statements, while loops and for loops with a simple game, the same building blocks this term uses.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
5.Understand
A condition is a yes/no question
A condition is something that is either true or false: distance < 20, button pressed, temperature > 28. Comparisons use symbols: < less than, > greater than, = equal to. Remember: 20 < 20 is false.
Two branches
IF the condition is true, THEN do one thing. ELSE (otherwise) do another. Exactly one branch runs every time the rule is checked, never both and never neither.
Choosing the ELSE
The ELSE matters as much as the THEN. 'IF dark THEN light on' with no ELSE means the light never goes off! Sometimes 'do nothing' is right, but it should be a choice you made, not something you forgot.
Explain it another way
IF it's raining THEN take a coat ELSE take sunglasses. You check the sky, get one answer, and do one thing.
A conditional evaluates a Boolean expression and transfers control to one of two blocks. In most programming languages it looks like: if (distance < 20) { stop(); } else { drive(); }
Robot vacuums have downward-facing 'cliff sensors' that measure the distance to the floor. IF that distance suddenly gets large (stairs) THEN stop and reverse ELSE carry on cleaning.
Worked example 1
A fan rule is: IF temperature > 26 °C THEN fan on ELSE fan off. What happens at 24, 26 and 30 °C?
24 > 26? False → fan off. 26 > 26? False → fan off (equal is not greater). 30 > 26? True → fan on.
Common mix-ups
- 'Less than 20' includes 20.
✓ It doesn't. If you mean 20 or less, write ≤ 20 (less than or equal to). - If the THEN part doesn't run, nothing happens.
✓ The ELSE part runs instead. If you leave it out, make sure 'nothing' really is safe.
6.Practice
Practice, not a test. Wrong answers get a hint and another try.
Practice 1
IF light level < 30 THEN lamp on ELSE lamp off. The sensor reads 45. What happens?
Practice 2
IF speed > 50 THEN beep. The speed is exactly 50. Does it beep?
Practice 3
A night light's only rule is: IF dark THEN lamp on. What goes wrong?
Practice 4 · a fresh version every time
Rule: IF distance < 30 cm THEN stop. The sensor reads 38 cm, 44 cm, 29 cm, 30 cm, 31 cm, 15 cm. For how many of these readings does the THEN branch run?
7.Apply it
Write IF–THEN–ELSE rules for a smart bin: it opens when a hand is within 15 cm and closes otherwise. Test your rule at 10, 15 and 20 cm. Did 15 cm do what you wanted? If not, change < to ≤.
8.Reflect
Concepts: Decisions and loops