๐ Lesson 2.3: Conditionals & Sensing
Up to now your cat has followed a script. Now it starts to think โ reacting to key presses, touches, and questions with "if this, then that." This is the moment a project becomes a game.
๐ฏ Learning Objectives
- Use if/then and if/then/else to make decisions
- Read Sensing blocks (touching, key pressed, mouse)
- Combine forever + if to check things constantly
- Make the cat move with the arrow keys and react to touches
Estimated Time: 40 minutes โข Builds on: Loops (2.2)
In This Lesson
๐ Learn It: The if/then block
An if/then block (orange Control category) says: "if something is true, then run the blocks inside." If it's not true, Scratch skips the inside and moves on. It's a fork in the road.
The pointy hexagon slot only accepts a true/false question โ called a condition. Those hexagon-shaped blocks come mostly from the Sensing category.
๐ Learn It: Sensing blocks
Sensing blocks are the cat's "senses" โ they answer questions about the world of your project.
| Sensing block | Answersโฆ |
|---|---|
| touching mouse-pointer? | Is this sprite touching the pointer (or another sprite/edge)? |
| key [space] pressed? | Is that key being held down right now? |
| mouse down? | Is the mouse button being clicked? |
| touching color? | Is the sprite touching a specific color (great for mazes)? |
Drop a hexagon Sensing block into the hexagon slot of an if and you've built a sentence: "if touching the pointer, thenโฆ"
๐ Learn It: forever + if โ the "game loop"
Here's the pattern behind nearly every Scratch game: a forever loop that constantly checks an if. Because the check repeats endlessly, the sprite reacts the instant something becomes true.
Now whenever the mouse touches the cat, it meows โ again and again โ because the forever loop never stops asking. Without the forever, Scratch would check just once at the start and never again.
๐ Learn It: if/then/else โ two paths
Sometimes you want one thing or another. The if/then/else block has two mouths: the top runs when the condition is true, the bottom when it's false.
Read it aloud: "If space is pressed, say Jump โ otherwise, say Waiting." Kids find "otherwise" a friendlier word than "else."
๐ ๏ธ Try It: Drive the cat with arrow keys
Let's make the cat controllable โ the foundation of every game. Build this on the cat:
- Add when ๐ฉ clicked, then a forever loop.
- Inside, add four if/then blocks, one per arrow key.
- In each, use Sensing's key [ ] pressed? and Motion's change x/y to nudge the cat.
Press the green flag and use the arrow keys โ the cat glides around the stage under your control. You just built player movement! ๐ฎ
๐ Cat won't move?
Click the stage once so it has "keyboard focus," then try the arrows. And double-check every change x/y block sits inside its if, which sits inside the forever.
๐ฌ Teach It: Decisions everywhere
Conditionals map perfectly onto how kids already think about rules.
Make it a call-and-response
"IF it's raining, THEN we bring an umbrella โ otherwise we don't. Give me an 'if/then' rule from your day!"
Kids fire back great ones: "If the light is green, then go." "If I finish veggies, then dessert." Each is a conditional.
Coach the arrow-keys build with questions
- "How does the cat know you pressed a key? Which color has 'senses'?" โ Sensing.
- "We want it to check all the time. What loop does that?" โ forever.
๐ง Ages 5โ7
One if/then is plenty: "if touching the pointer, meow." Skip the four-arrow build or do it together, one arrow at a time.
๐ง Ages 8โ11
The arrow-keys project is a big hit. Then challenge: "Make it say something if it touches the edge."
๐ง Ages 12+
Introduce Operators (green) to combine conditions: "if touching edge and key space pressed." Real logic!
โ ๏ธ Common kid bug
They use a single if with no loop and wonder why it only checks once. The fix is almost always "wrap it in forever." Make that your shared catchphrase.
๐ฏ Quick Check
Question 1: What shape of block fits in an "if ___ then" slot?
Question 2: Why do most games wrap their "if" checks in a forever loop?
Question 3: "if/then/else" is best whenโฆ
Summary & Module 2 Complete ๐
๐ Key Takeaways
- if/then runs blocks only when a condition is true; if/then/else gives a second path.
- Conditions are hexagon Sensing blocks โ touching, key pressed, mouse down.
- forever + if is the pattern behind interactive projects and games.
- Wrap checks in a loop, keep blocks nested correctly, and read conditions aloud as sentences.
๐ Module 2 done!
Sequences, loops, and conditionals โ you now know the three ideas that underpin all programming, in any language.
๐ What's next
Module 3: Building Real Projects puts it all together into things kids proudly share โ an animated story, then a real game with a score. See you there.