Lesson 4: Conditionals in Scratch
Estimated time: 15-20 minutes | Scratch Activity
What You Will Learn
By the end of this lesson, your child will be able to:
- Find and use the "if/then" block in Scratch
- Make a sprite respond to key presses using conditionals
- Make a sprite react when it touches a color
- Create a simple interactive story with choices
If/Then Blocks in Scratch
You have been practicing if/then thinking with words, games, and drawings. Now you get to use if/then in actual code! Scratch has a special block for this. It is in the Control category (the yellow-orange blocks).
Conditional -- A coding instruction that checks a condition and only does something if that condition is true. The Scratch "if/then" block is a conditional. The code inside it only runs when the condition is true.
Finding the If/Then Block
Open Scratch (scratch.mit.edu, click "Create"). Look at the block categories on the left side.
- Click Control (yellow-orange category).
- You will see a block shaped like a mouth: "if < > then". It has a diamond-shaped hole where you put the condition to check.
- You will also see "if < > then / else" -- this one has two mouths: one for what happens when the condition is true, and one for when it is false.
- The diamond-shaped conditions come from the Sensing category (light blue) and the Operators category (green).
Project 1: Move the Cat with Arrow Keys
Let us make the Scratch cat move when you press the arrow keys. This uses if/then blocks to check which key is being pressed.
Scratch Blocks to Use
You need a "forever" loop with if/then blocks inside it. This keeps checking for key presses over and over.
forever
if <key [right arrow] pressed?> then
change x by (10)
end
if <key [left arrow] pressed?> then
change x by (-10)
end
if <key [up arrow] pressed?> then
change y by (10)
end
if <key [down arrow] pressed?> then
change y by (-10)
end
end (forever)
How to build it:
- Drag "when green flag clicked" from Events.
- Drag "forever" from Control and snap it below.
- Drag "if < > then" from Control and put it inside the forever loop.
- Go to Sensing (light blue). Drag "key [space] pressed?" into the diamond hole. Change "space" to "right arrow."
- Go to Motion (blue). Drag "change x by (10)" inside the if/then mouth.
- Repeat for left arrow (change x by -10), up arrow (change y by 10), and down arrow (change y by -10).
Click the green flag and use your arrow keys to move the cat around the stage!
Talk About It
Ask your child: "How many if/then blocks did we use?" (Four -- one for each arrow key.) "What would happen if we took out the 'if key right arrow pressed' block?" (The cat would not move right anymore.) This shows that each if/then block handles one specific condition.
Project 2: Color Detection
Scratch can check if a sprite is touching a certain color. This is great for making mazes, games, and art projects.
Scratch Blocks to Use
First, paint a simple backdrop: click the Stage area (bottom right), go to Backdrops, and use the paint tools to draw a green rectangle on the stage. Then build this code for the cat:
forever
if <touching color [green]?> then
say [I am on the grass!] for (2) seconds
end
end (forever)
How to set the color: Click the color square in the "touching color" block, then use the eyedropper tool to click on the green area of your backdrop. This tells Scratch exactly which color to look for.
Now use the arrow key code from Project 1 to move the cat. When the cat walks onto the green area, it will say something!
Try It: Add More Colors
Paint more colored areas on your backdrop (blue for water, brown for a path, yellow for sand). Add more if/then blocks so the cat says something different for each color:
- Touching blue? "Splash! I am in the water!"
- Touching brown? "I am on the path."
- Touching yellow? "The sand is warm!"
You are building a simple world that your sprite can explore and react to!
Project 3: Interactive Story with Choices
Remember the "Choose Your Own Adventure" decision trees from Lesson 3? Now you can make one in Scratch! The sprite will ask a question, and the player picks an answer.
Scratch Blocks to Use
We will use the "ask and wait" block from Sensing, which lets the player type an answer.
say [Welcome to my adventure!] for (2) seconds
ask [You find a door. Do you open it? Type yes or no.] and wait
if <(answer) = [yes]> then
say [You open the door and find a friendly dragon!] for (3) seconds
switch backdrop to [castle]
else
say [You walk away and find a garden full of flowers.] for (3) seconds
switch backdrop to [garden]
end
How to build it:
- Start with "when green flag clicked" and a "say" block for the introduction.
- From Sensing, drag "ask [ ] and wait" and type your question.
- From Control, drag the "if < > then / else" block (the one with two mouths).
- From Operators (green), drag the "( ) = ( )" block into the diamond hole.
- From Sensing, drag "answer" into the left side of the equals block. Type "yes" on the right side.
- Add "say" blocks in each mouth for what happens with each choice.
- Optional: add different backdrops from the Scratch library for each path.
Parent Tip
This project has more pieces than the earlier ones. It is perfectly fine to build it together, with you handling the trickier parts (like the operators block) while your child picks the story content. The important thing is that they understand the concept: the program checks the answer and does different things depending on what the player typed. That is a conditional in action!
The Big Idea: Programs That Respond
Before this lesson, your Scratch programs did the same thing every time you pressed the green flag. Now, with if/then blocks, your programs can respond to what is happening:
- Respond to the keyboard: If a key is pressed, do something
- Respond to the world: If the sprite is touching a color, react
- Respond to the player: If the player types "yes," go one way; if "no," go another
This is what makes programs interactive. Every game you have ever played, every app you have ever used -- they all use conditionals to respond to what you do. And now you know how to build them!
Think About It
Everything you learned in this module connects:
- If/then thinking from Lesson 1 = the idea behind conditionals
- Yes/no questions from Lesson 2 = the conditions you check in code
- Decision trees from Lesson 3 = the branching paths in your programs
- Scratch conditionals from this lesson = putting it all together in real code
Bonus Challenge
Make It Your Own
Combine what you learned! Create a project that uses:
- Arrow key movement (from Project 1)
- Color detection (from Project 2)
- A message that appears when the sprite reaches a certain spot
Idea: Make a simple maze! Paint walls on the backdrop in one color. Use "if touching color" to detect when the cat hits a wall (and maybe say "Ouch!" or bounce back). Paint a gold star at the end, and when the cat touches it, say "You win!"
Check Your Understanding
1. What does the "if/then" block in Scratch do?
2. What is the difference between "if/then" and "if/then/else" in Scratch?
3. Why do we put if/then blocks inside a "forever" loop?
Key Takeaways
- A conditional is a coding instruction that checks a condition and only runs when it is true
- The if/then block in Scratch checks a condition (like "key pressed?" or "touching color?")
- The if/then/else block handles both true and false -- it does one thing or the other
- Put if/then blocks inside a forever loop so they keep checking
- Conditionals make programs interactive -- they respond to what the user does
- Every game and app uses conditionals to make decisions
What is Next?
Practice Activities
More decision-making activities and Scratch conditional challenges to build your skills.
Practice ActivitiesModule Quiz
Test what you know about if/then thinking, yes/no questions, decision trees, and conditionals.
Take the QuizModule 5
In Module 5, you will learn about loops and repetition -- how to make things happen over and over without copying blocks!
Go to Module 5