Skip to main content

๐Ÿ”€ 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)

Ages 5โ€“7 (with help) Ages 8โ€“11 Ages 12+
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.

if touching edge? then
say Ouch! for 1 seconds

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.

The Sensing category palette showing hexagon question blocks like 'touching mouse-pointer?', 'touching color?', 'key pressed?', and reporter blocks like 'mouse x', 'mouse y', and 'timer'.
Figure 1: The Sensing palette. Hexagon blocks are true/false questions; ovals report values.
Sensing blockAnswersโ€ฆ
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.

when โš‘ clicked
forever
if touching mouse-pointer? then
start sound Meow

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.

flowchart TD A["forever: keep checking"] --> B{"touching pointer?"} B -->|Yes| C["meow"] B -->|No| A C --> A style A fill:#FFAB19,color:#5c3d00,stroke:#333 style B fill:#5CB1D6,color:#fff,stroke:#333 style C fill:#CF63CF,color:#fff,stroke:#333

๐ŸŽ“ 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.

if key space pressed? then
say Jump! for 1 seconds
else
say Waitingโ€ฆ for 1 seconds

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:

  1. Add when ๐ŸŸฉ clicked, then a forever loop.
  2. Inside, add four if/then blocks, one per arrow key.
  3. In each, use Sensing's key [ ] pressed? and Motion's change x/y to nudge the cat.
when โš‘ clicked
forever
if key right arrow pressed? then
change x by 10
if key left arrow pressed? then
change x by -10
if key up arrow pressed? then
change y by 10
if key down arrow pressed? then
change y by -10

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.

๐Ÿ  Back to Course Home