Learning Goals 3 min
By the end of this lesson you will be able to:
- Recognise the if-then-else C-block in the Control palette and explain why it has two body slots instead of one.
- Predict which branch — the "then" or the "else" — will run for any given boolean question, and confirm that exactly one branch runs every time (never both, never neither).
- Use if <> then ... else ... together with set [color v] effect to () to make a sprite that looks different depending on where it is or what it's touching.
Warm-Up — two scripts that should be one 7 min
Priya is writing a script. She wants the cat to be red when it's on the right half of the Stage and blue when it's on the left half. Last week she only knew about if <> then, so she wrote two of them:
when flag clicked
forever
if <(x position) > (0)> then
set [color v] effect to (0)
end
if <(x position) < (0)> then
set [color v] effect to (100)
end
end
This works, but Priya has a feeling there's a cleaner way. What's annoying about writing it as two if-thens?
Reveal the answer
The two questions x > 0 and x < 0 are opposites. Scratch checks both every loop tick, even though only one can ever be true. (What about x = 0? Neither runs!) The code reads "is it the right side? do red. is it the left side? do blue." — but the real thought is "is it the right side? if yes do red, if no do blue." That's one decision, not two.
Today we meet the block that fits that thought exactly: one question, two slots, exactly one runs.
New Concept — the if-then-else C-block 15 min
Last lesson you met if <> then — a C-block with one diamond and one body slot. The blocks inside run if the diamond reports true; otherwise nothing happens. The "otherwise" was silent.
Today's block makes the "otherwise" loud.
Where to find it
In the orange Control palette, right below the plain if-then, is a bigger C-block with the word else in the middle:
if <> then
else
end
It looks like two C-blocks fused together. That's almost what it is:
- The top slot (above the word "else") is the "yes" branch — runs only when the diamond reports true.
- The bottom slot (below the word "else") is the "no" branch — runs only when the diamond reports false.
Exactly one runs. Always.
This is the rule that makes if-then-else so useful. Every time Scratch reaches the block, it asks the diamond question, gets back true or false, and runs exactly one of the two slots:
- Never both. The "yes" branch and the "no" branch are mutually exclusive.
- Never neither. Even if the question is weird (like
x = 0from the warm-up), it still reports either true or false, so one slot always runs.
The branch picture
Imagine a path in the forest that splits in two. You walk up to the fork and ask "is the left path muddy?" If yes, you take the right path. If no, you take the left. You don't take both. You always take one. That's the mental model — your script branches.
if <(x position) > (0)> then
set [color v] effect to (0)
else
set [color v] effect to (100)
end
x = 0 falls into the "no" branch — Scratch counts 0 > 0 as false.Why it's better than two if-thens
Three reasons:
- One question, one answer. Scratch only checks
x > 0once per loop tick, not twice. Faster, even if you can't feel it. - The two outcomes are visibly linked. Reading the code, you see "yes does this, no does that" — they're in the same block, stacked together.
- No gap. With two if-thens, you could accidentally write conditions that both miss (like
x > 0andx < 0both missing whenx = 0). With if-then-else, one branch always runs.
Nesting inside a forever
Just like plain if-then, an if-then-else usually lives inside a forever loop so the question gets re-asked many times a second:
when flag clicked
forever
if <(x position) > (0)> then
set [color v] effect to (0)
else
set [color v] effect to (100)
end
end
Worked Example — the seeker shouts Warmer or Colder 12 min
Back to our Hot or Cold project. The seeker already patrols. Today it shouts two ways. For now the treasure side is simply the right half — anywhere x > 0. Open your saved project and build it in seven steps.
Step 1 — Open the project
Open your saved Hot or Cold project. The seeker still patrols. Click the seeker sprite.
Step 2 — Start a fresh stack
From Events: when ⚑ clicked. From Control: forever under it.
Step 3 — Add the if-then-else
From Control: if <> then ... else .... Drop it inside the forever C. The loop expands to fit.
Step 4 — Ask which side it is on
From Operators: () > (). Drop it on the diamond slot. From Motion: drag x position into the left slot. Type 0 in the right slot.
Step 5 — Fill the "yes" branch
From Looks: say [Warmer!] for (1) seconds. Drop it in the top slot — the warmer-side reaction.
Step 6 — Fill the "else" branch
From Looks: say [Colder!] for (1) seconds. Drop it below the word "else" — the colder-side reaction.
Step 7 — Click the flag and drag the cat
Drag the seeker across the centre line with your mouse. On the right it shouts "Warmer!". On the left it shouts "Colder!". One side, then the other — never both, never silent.
The full assembled stack
when flag clicked
forever
if <(x position) > (0)> then
say [Warmer!] for (1) seconds
else
say [Colder!] for (1) seconds
end
end
What you just built: the same branching pattern that decides "is the player alive? show the game, else show Game Over." Right now closeness is a rough left/right guess. In L02-08 the seeker measures real distance to the treasure — and "Warmer" gets honest.
Try It Yourself — three if-then-else drills 15 min
Goal: Make a cat that says Yay! for one second when the space key is pressed, and Boo for one second when it's not. Use one if-then-else inside a forever.
when flag clicked
forever
if <key [space v] pressed?> then
say [Yay!] for (1) seconds
else
say [Boo] for (1) seconds
end
end
Think: The cat is "talking" constantly — one branch always runs. Compare to last lesson's space-key script that only said something when pressed. The else is the difference.
Goal: Make a cat that grows to size 150 when the mouse is touching it, and shrinks back to size 100 when it isn't. Use set size to () % in both branches.
when flag clicked
forever
if <touching [mouse-pointer v] ?> then
set size to (150) %
else
set size to (100) %
end
end
Think: Hover and the cat puffs up. Move away and it deflates. The else branch is just as important as the yes branch — without it, the cat would grow once and stay big forever, because nothing tells it to shrink back.
Goal: Build a "traffic light" sprite. Use a variable signal (you'll meet variables properly in L02-31, but for now ask your teacher to add one). When signal is greater than 5, set colour effect to 0 (red-ish). Otherwise, set it to 70 (green-ish). Then add a key press: press key [s v] pressed? to flip the variable between 1 and 10.
when flag clicked
set [signal v] to (1)
forever
if <(signal) > (5)> then
set [color v] effect to (0)
else
set [color v] effect to (70)
end
if <key [s v] pressed?> then
set [signal v] to (10)
end
end
Think: The traffic light reads a number and picks a colour. The number changes when you press s. The light reacts within a frame. This is the heart of every state-machine you'll ever build — a variable, a question, a branch.
Mini-Challenge — Aljay's broken switch 5 min
"The cat never stops being red"
Aljay wants a cat that's red while the space key is held down, and back to normal the moment you let go. He writes:
when flag clicked
forever
if <key [space v] pressed?> then
set [color v] effect to (0)
end
if <key [space v] pressed?> then
set [color v] effect to (50)
end
end
Click the flag mentally — what does the cat do, and what does Aljay want?
Reveal one valid solution
Aljay wrote two if-thens that ask the same question. When space is pressed, the first sets colour to 0, then the second immediately sets it to 50. When space is not pressed, neither runs and the cat stays whatever it was last. The cat never returns to "no effect" by itself.
What Aljay actually wanted was one if-then-else — when space is pressed, do red; otherwise, do normal:
when flag clicked
forever
if <key [space v] pressed?> then
set [color v] effect to (50)
else
set [color v] effect to (0)
end
end
One question. Two outcomes. The "otherwise" path is what made the cat come back to normal — and Aljay's original stack didn't have one. If you ever write the same question twice in a row, an if-then-else is almost always cleaner.
Recap 3 min
The if <> then else block is a fork in your script. It asks one boolean question and runs exactly one of two body slots — the "yes" branch above the word "else", or the "no" branch below it. Never both, never neither. Compared to last lesson's plain if <> then (where "no" meant "do nothing"), if-then-else makes the "otherwise" path visible and guarantees one branch always runs. Whenever you find yourself writing two if-thens with opposite questions, you almost always want one if-then-else instead.
- Branch
- One of the two paths a script can take inside an if-then-else. Every run of the block picks exactly one branch.
- Else
- The keyword in the middle of the if-then-else block that separates the "yes" body from the "no" body. Means otherwise — not and also.
- Mutually exclusive
- Two outcomes that can't both happen at the same time. The yes and else branches of an if-then-else are mutually exclusive.
- Boolean reporter
- Same as last lesson — a hexagonal block that reports true or false. The diamond slot of if-then-else only accepts these.
- Branchless
- What you get if you put the same block in both branches: the if-then-else does nothing useful and should be removed. The block runs every tick regardless.
Homework 2 min
The Mood Cat. Build one cat that reads four different things about the world and changes its appearance accordingly. Use at least three if-then-else blocks.
- If the cat is touching the right edge (touching [edge v]?), turn its colour effect to 100. Otherwise back to 0.
- If the mouse pointer is touching the cat, grow to size 150. Otherwise size 100.
- If the space key is pressed, change to costume costume2. Otherwise costume costume1. (Use switch costume to [costume2 v].)
- Put all three if-then-elses inside one forever loop so they all run together every frame.
- Bonus: add a fourth if-then-else of your own invention. Think of a question the cat could ask about the world (touching a sprite, key pressed, x-position) and a difference it should make in how the cat looks.
Save as HW-L2-07-Mood-Cat.sb3. Click the flag. Then try every combination — move the mouse, press space, drag the cat to the edge — and see all four moods in action at once.
Bring back next class:
- The
.sb3file. - Your answer to: "In one of your if-then-else blocks, what would happen if you swapped the contents of the 'yes' slot and the 'else' slot? Try it — describe the change in one sentence."
Heads up for next class: SCR-L02-08 zooms in on the three comparison operators — () > (), () = (), () < () — that build the hexagonal questions you've been dropping into diamond slots.