Learning Goals 3 min
By the end of this lesson you will be able to:
- Recognise the if-then C-block in the Control palette and explain what its diamond slot is for.
- Drop a boolean reporter (a hexagonal block with a true/false answer) into the diamond and predict whether the inside will run.
- Use if <> then + touching [edge v]? to make the treasure-hunt seeker react to its surroundings instead of moving blindly.
Warm-Up — the cat that won't stop 7 min
You built dozens of cat-walks-across-the-Stage scripts in Level 1. Here's the smallest one — and a problem.
when flag clicked
forever
move (10) steps
end
Click the flag. What happens?
Reveal the answer
The cat walks to the right edge of the Stage and then keeps trying to walk. Scratch clamps the sprite's x-coordinate at the edge, so the cat looks frozen with one foot off the screen, jittering. Forever loops never decide when to stop — that's the whole point of "forever". The script has no decision in it.
Our treasure-hunt seeker needs to look at the world (am I touching the fence?) and react (turn around) — instead of blindly walking off the Stage. Today's block adds that decision.
New Concept — the if-then C-block 15 min
Up to now, every block in your scripts has run no matter what. Move 10 steps? It moves, every single time. Say "Hi!"? It says, every single time. Today we meet the first block that runs only sometimes — only when a question is true.
The block we are studying today
| Block | Category | What it does |
|---|---|---|
| if <> then | Control | Runs the blocks inside its C only if the hexagonal question is true. If the answer is false, the inside is skipped. |
| touching [edge v]? | Sensing | A boolean reporter — reports true while the sprite touches the Stage edge, false otherwise. |
Where to find it
Open the orange Control palette. Scroll past the loops. You'll see a C-shaped block with a diamond-shaped hole near the top:
if <> then
end
The block does nothing on its own. It needs two more things:
- Something to drop into the diamond — a yes/no question, called a boolean reporter.
- One or more blocks inside the C — the action that should happen only if the answer is yes.
The diamond and the hexagon
Scratch is shape-coded. Notice:
- The diamond slot in if-then is a hexagonal hole. Only blocks that report true/false fit there.
- The true/false blocks — like touching [edge v]?, key [space v] pressed?, () = () — are themselves hexagonal.
Same shape, same place. Try dragging a circle reporter (like x position) into the diamond — Scratch refuses, because circles report numbers, not yes/no answers.
Drop it in
Drag touching [edge v]? from the Sensing palette into the diamond slot of the if-then block. It should snap in and turn the diamond into a filled hexagon:
if <touching [edge v] ?> then
say [Ow!] for (1) seconds
end
Putting it inside a loop
A bare if-then runs once. It checks the question once, acts (or doesn't), and that's it. The real magic happens when you put it inside a forever loop — now the seeker re-asks the question many times a second.
when flag clicked
forever
move (10) steps
if <touching [edge v] ?> then
turn cw (180) degrees
end
end
That's the smallest meaningful Scratch game loop: do something, check something, react. Forever. Every game you'll ever build in Scratch — collision detection, score updates, key-listening, win conditions — is some version of this pattern.
Worked Example — the seeker that stays on the field 12 min
We start the Hot or Cold project today. First job: a seeker cat that patrols the playfield and never gets stuck on the fence. Open Scratch and build it in eight steps.
Step 1 — Start fresh
New Scratch project. Default cat — this is our seeker. Drag it to roughly x: −160, y: 0 on the Stage.
Step 2 — Drop the hat
From Events: when ⚑ clicked.
Step 3 — Add the forever loop
From Control: forever. Snap it under the hat. The C-shape opens.
Step 4 — Walk inside the loop
From Motion: move (10) steps. Drop it inside the forever C.
Step 5 — Click the flag to see the bug
The seeker walks to the right fence and gets stuck, just like the warm-up. Now we fix it.
Step 6 — Add the if-then
From Control: if <> then. Drop it inside the forever, below the move block.
Step 7 — Drop the sensing question into the diamond
From Sensing: touching [edge v]?. Drag it onto the diamond slot of the if-then. Drop. It snaps in.
Step 8 — Add the reaction
From Motion: turn cw (180) degrees. Drop it inside the if-then C. (You can also use point in direction (90) — there's more than one right answer.)
Click the flag. The seeker walks right, hits the fence, turns around, walks left, hits the fence, turns around — forever. The cat is now reacting to the world instead of doing one thing blindly.
The full assembled stack
when flag clicked
forever
move (10) steps
if <touching [edge v] ?> then
turn cw (180) degrees
end
end
What you just built: the same pattern that runs Pac-Man, Pong, Asteroids, and every catch-the-falling-thing game ever made. A loop, a question, a reaction. Everything else is bigger or fancier versions of this.
Try It Yourself — three if-then drills 15 min
Goal: Build a script that makes the seeker say Ouch! for one second only when the space key is pressed. Wrap it in a forever loop so the question is asked over and over.
when flag clicked
forever
if <key [space v] pressed?> then
say [Ouch!] for (1) seconds
end
end
Think: If you skip the forever, you'd have to press space within the first millisecond of the flag click. With the loop, the cat checks every frame.
Goal: Make the seeker glow only when it reaches the treasure's side of the Stage (the right half — anywhere x > 0). Use () > () from Operators with x position from Motion.
when flag clicked
forever
if <(x position) > (0)> then
set [color v] effect to (50)
end
end
Think: You're meeting a comparison operator early — () > () reports true when the left number is bigger. L02-08 zooms right in on these.
Goal: Build a treasure sprite that hides itself the moment the mouse pointer touches it — like finding the prize. (Sensing → touching [mouse-pointer v]?.) Bonus: add a when ⚑ clicked + show at the top so every new game starts with the treasure visible.
when flag clicked
show
forever
if <touching [mouse-pointer v] ?> then
hide
end
end
Think: The treasure has gone from a passive picture to a tiny game object — find me and I vanish. This three-block change is the difference between a slideshow and a game.
Mini-Challenge — the seeker that meows at the fence 5 min
"Daniel's bouncing seeker"
Daniel wants the seeker to patrol back and forth, and say "Meow!" for half a second every time it bounces off a fence. He writes this:
when flag clicked
forever
move (10) steps
say [Meow!] for (0.5) seconds
if <touching [edge v] ?> then
turn cw (180) degrees
end
end
Click the flag mentally — what happens, and what does Daniel actually want?
Reveal one valid solution
Daniel's stack says "Meow!" every frame, not "only when bouncing". The say block sits outside the if-then, so it runs every time around the forever loop — which is many times a second. The cat ends up with a permanent speech bubble.
The fix is to move the say inside the if-then, so it only fires when the touching-edge question is true:
when flag clicked
forever
move (10) steps
if <touching [edge v] ?> then
say [Meow!] for (0.5) seconds
turn cw (180) degrees
end
end
Same six blocks. One was in the wrong place. The if-then C is a fence — anything inside happens conditionally, anything outside happens always. Knowing where the fence is, is the whole skill.
Recap 2 min
You met your first decision block. The if <> then C-block in the Control palette holds blocks that should run only sometimes — only when the hexagonal question in its diamond reports true. Inside a forever loop, an if-then turns a blind script into one that watches its surroundings and reacts. The fence between "inside the C" and "outside the C" is the most important boundary in conditional code.
- Conditional
- A block (or pattern) that runs other blocks only if a condition is true. The if <> then block is Scratch's smallest conditional.
- Boolean reporter
- A hexagonal block that reports either true or false. Examples: touching [edge v]?, key [space v] pressed?, () = ().
- C-block
- A block shaped like a C with one or more slots for other blocks inside the C. if <> then, forever, and repeat (10) are all C-blocks.
- Diamond slot
- The hexagonal hole at the top of an if-then where only boolean reporters fit. The block shape physically prevents you from using the wrong kind of input — that's intentional.
Homework 1 min
The Three-Reactor Drill. Build three sprites in one project. Each one reacts to a different thing.
- Sprite 1: patrols forever (forever + move (10) steps), and turns around when it touches the edge (if <> then + touching [edge v]?).
- Sprite 2: hides forever, but shows for one second every time the space key is pressed (key [space v] pressed?). Then hides again. Use wait (1) seconds after the show.
- Sprite 3: stays still, but say [Hi!] for (1) seconds every time the mouse pointer touches it (touching [mouse-pointer v]?).
Save as HW-L2-06-Three-Reactors.sb3. Hit the flag — all three sprites should be doing their things at the same time, each reacting to its own question.
Bring back next class:
- The
.sb3file. - Your answer to: "Sprite 1's question is checked over and over. What would happen if you put the if-then before the move instead of after? Try it and tell me."
Heads up for next class: SCR-L02-07 meets if <> then else — the same C-block but with two body slots, one for "yes" and one for "no". That's how the seeker learns to shout "Warmer!" or "Colder!".