Class 6 · Unit 5: Computational thinking · Lesson 5.4 · 40 min
If this, then that
Snowing? Pheran and gumboots. Road closed? Bus at the main stop. Otherwise, walk. Muteeb's mother never repeats herself — she gave him rules.
Today you will: Turn everyday rules into if-then · Build a Scratch maze that reacts to walls · Find out what a program really "sees"
Story
Every morning, Muteeb's mother says the same thing:
If it is snowing, then wear your pheran and gumboots. If the road is closed, then wait for the school bus at the main stop. Otherwise, walk the usual way.
Muteeb does not need a new instruction every day. His mother gave him rules with conditions, and he checks them himself each morning.
Computers work the same way. A program cannot "see" the weather, but it can check a condition, if this is true, then do that, and choose what to do next.
Watch
How programs make decisions — scan code 6.5.4 in the printed book.
Learn it
Muteeb's mother gave him rules with conditions: if it is snowing, then wear the pheran. Every morning he checks the condition and decides for himself. A program does the same. It cannot see the weather, but it can check a condition, something that is either true or false, and choose what to do. That is how a program makes decisions without a person telling it each time.
Words to know
- Condition a question with a true or false (yes or no) answer, like "Is it snowing?" or "Is the sprite touching black?"
- If–then do something only when the condition is true.
- If–then–else two paths: one for when the condition is true, and the else path for when it is false.
- Flowchart a drawing of steps. A question is drawn as a diamond, with a Yes arrow and a No arrow.
- Forever loop a loop that never stops, so the program keeps checking its conditions.
Step by step: a kettle that decides
An electric kettle has one rule:
forever
if water is boiling then
switch off
else
keep heating
- The condition: is the water boiling? It can only be true or false.
- True: the kettle switches off.
- False: the else path runs, and it keeps heating.
- Forever: it keeps checking, so it notices the moment the water boils.
In Scratch the same shape looks like if <touching color [black]?> then … else …, inside a
forever loop.
Many if–thens in a row make a decision tree (Lesson 3.4), which is how many simple AI systems decide: Is the apple red? Is it bigger than 7 cm? Are there spots?
🕌 From our heritage
Remember al-Khwārizmī from Lesson 1.1? In Baghdad, around 825 CE, he wrote a book showing how to calculate with Indian numerals, step by careful step. Translated into Latin as Algoritmi de numero Indorum, his name became our word algorithm. (His other famous book, on al-jabr, gave us the word algebra.) The step-by-step rules you are building in Scratch today are that same idea, 1,200 years later.
Sources: al-Khwārizmī, Kitāb al-Ḥisāb al-Hindī, c. 825 CE, known in Latin as Algoritmi de numero Indorum; and Kitāb al-Mukhtaṣar fī Ḥisāb al-Jabr wa'l-Muqābala, c. 820 CE.
Careful: an if block checks only at the moment the program reaches it. Put it inside a
forever loop, or it checks once and never again. And the computer does not know what a wall
is, only its colour: paint the goal black and the sprite treats it as a wall.
On the laptop
You need: Scratch Desktop (offline, no account) — or scratch.mit.edu without signing in
Mission 1: Maze and controls (pairs, 8 min)
- ☐1
Click the Stage, then Backdrops → Paint. Draw thick black walls and a small green goal box.
- ☐2
Pick a small sprite (set size 30). Add four scripts:
when [right arrow] key pressed → change x by 5, and the same for left (x by -5), up (y by 5) and down (y by -5).
Mission 2: Add the decisions (pairs, 10 min)
- ☐3
Under when green flag clicked:
go to x: (start x) y: (start y), then a forever loop holding two rules:if <touching color [black]?> then say [Ouch! Wall!] for 0.5 seconds go to x: (start x) y: (start y) if <touching color [green]?> then say [I made it!] stop [all]Use the eyedropper in the colour box to pick your exact colours.
- ☐4
Predict: what will happen when you steer into a wall on purpose? Then try it.
- ☐5
Play your partner's maze. Can you reach the goal?
Finished early?
Add an if-else: if the sprite touches blue water, it says Splash! and slows
down; else it moves normally.
No laptop today?
Draw a grid maze on paper. Your partner is the sprite and follows your rules out loud: "If the next square is a wall, then stay and say Ouch." Then draw your rules as a flowchart with diamond-shaped question boxes.
Debate it
A school gate machine lets a student in if they show an ID card. One day a child forgets their card. It is freezing outside and the machine says No. Should a machine's rule ever be broken? Who should be able to say "let them in anyway"?
Check yourself — practice, not a test
1. A condition in a program can only be true or false.
- ○ True
- ○ False
2. What does this rule do? if <touching color black?> then go to start
- ○ It paints the wall black
- ○ It sends the sprite back to the start when it touches a black wall
- ○ It stops the program forever
- ○ It moves the sprite 5 steps
3. Put these steps of a morning rule in order:
- 1. If yes, wear gumboots
- 2. Look outside
- 3. Check: is it snowing?
- 4. Walk to school
4. An if-then-______ block gives the program a second path for when the condition is false.
Remember
If this, then that: conditions let a program decide.
Typing practice
Practise the arrow keys and space bar. You will steer a sprite with them today.
- typing.com: Beginner → Space bar lessons, then free practice
- Then type this sentence twice, with no mistakes:
if it is snowing then wear boots