TactivergeLite

Lite / Discovering How the World Works / Week 22

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 · Reviewed by the founders' mentors · Full interactive version

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.

Predict

Rule: IF distance < 20 cm THEN stop ELSE drive. The sensor reads exactly 20 cm. What does the robot do?

  1. Stop
  2. Drive
  3. Nothing, it's confused
Guess first, then reveal

It drives. 20 < 20 is false (20 is not less than itself), so the ELSE branch runs. Machines never get confused, but they do follow the exact comparison, and edge values like this catch people out all the time.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“Programming Basics: Statements & Functions: Crash Course Computer Science #12” by CrashCourse (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

IF it's raining THEN take a coat ELSE take sunglasses. You check the sky, get one answer, and do one thing.

Technical

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(); }

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

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?

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 ≤.

Reflect

When you make a decision with 'if', do you always think about the 'else'?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Debugging: find the step that went wrongChoosing a good threshold →