If-Then: Teach Your Sprite to Decide
Kids learn how if-then helps code make choices, then build Catch the Star, their first real Scratch game.
CarnielSeriesCoding for Children5 of 5
On this page15 sections
Hi again, coder! Last time your sprite learned to listen. You pressed the arrow keys and it moved. You clicked it and it giggled.
Today your sprite learns to think. It will look around, ask a question, and decide what to do. By the end, you will have built your very first game: Catch the Star.
You Make Choices All Day
Every day, you make lots of little choices without even noticing:
- If I'm hungry, then I eat a snack.
- If the light is red, then we stop the car.
- If it's raining, then I take an umbrella.
Each one starts with a question that has a yes or no answer. Am I hungry? Is the light red? Is it raining? If the answer is yes, you do the thing. If it's no, you don't.
Coders call this if-then. The question part is called a condition.
Sometimes you want to do something different when the answer is no. That's called if-then-else. If it's raining, take an umbrella. Else (if it's not raining), wear sunglasses.
Warm-Up Game: Yes or No?
Play this with a friend or a grown-up. One person calls out if-then rules, and everyone checks the condition:
- "If you're wearing something blue, then touch your nose."
- "If you have a pet, then stand on one foot."
- "If your name has the letter A in it, then clap twice. Else, stomp once."
Everyone hears the same rules but does different things, because the answers are different for each person. Your sprite works the same way!
Meet the If-Then Block
Open your project from last time. (Click File, then Load from your computer.) Your sprite should already move with the arrow keys. If you're starting fresh, build the four arrow-key scripts from Episode 4 first.
Click the orange Control circle. Find the block that says if < > then. See the pointy, empty space after if? That's where the condition goes.
Conditions are pointy-shaped blocks, and many of them live in the light blue Sensing group. Sensing blocks let your sprite notice things, like "Am I touching the mouse?" or "Is the space key pressed?"
Build Catch the Star
Here's the game: you drive your sprite with the arrow keys. A star sits somewhere on the stage. When you catch it, it changes color and jumps to a new spot. Then you chase it again!
Step 1: Add a star
At the bottom right of the screen, under the stage, find the round button with a cat face and a plus sign. That's Choose a Sprite. Click it, type star in the search box, and click the Star. Now you have two sprites!
Click the star in the sprite list so it's highlighted. Any code you build now belongs to the star, not your first sprite.
Step 2: Start somewhere random
Drag out when green flag clicked from Events. Under it, snap go to random position from Motion. (If the block says go to mouse-pointer, click the little arrow and choose random position.)
Step 3: Keep checking, forever
Snap a forever block under that. The star needs to keep checking again and again, because you could catch it at any moment.
Step 4: Add the if-then
Drag the if < > then block inside the forever loop.
Now click the light blue Sensing circle. Drag touching mouse-pointer ? into the pointy space after if. Click its little arrow and choose Sprite1. That's the name Scratch gave your first sprite. (If you renamed it, pick that name instead.)
Step 5: What happens when you catch it
Put these two blocks inside the if-then block:
- change color effect by 25 from purple Looks
- go to random position from blue Motion
Read your code out loud: Forever, if I'm touching Sprite1, then change color and jump somewhere new. It sounds just like a rule from the warm-up game!
Play Your Game!
Click the green flag. Use the arrow keys to drive your sprite to the star. The moment they touch, the star changes color and pops up somewhere else. Chase it again!
Not working? Time to debug. Check these one at a time:
- Is the star's code on the star? Click the star in the sprite list to see its code.
- Is the if-then block inside the forever loop?
- Does the touching block say Sprite1 (or your sprite's name), not mouse-pointer?
- Did you click the green flag?
Challenges
- Celebrate! Inside the if-then, add a start sound block from Sound. If the list is empty, click the Sounds tab at the top and pick one from the library.
- Talking star: add say Got me! for 1 seconds inside the if-then, before the star moves.
- Try if-then-else: on your first sprite, build forever, if touching mouse-pointer, then say Hi! else say (leave the words empty). Move your mouse over the sprite and away again. Watch it switch!
- Danger zone: add a third sprite, like a crab. Give your first sprite a rule: if touching Crab, then say Ouch! and go to x: 0 y: 0. Now there's something to avoid.
New Words
- If-then: code that only runs when a condition is true.
- If-then-else: code that does one thing when a condition is true and something else when it's false.
- Condition: a yes-or-no question, like "Am I touching the star?"
- True and false: the computer's way of saying yes and no.
- Sensing: blocks that let a sprite notice what's around it.
Quick Quiz
- What kind of question goes in a condition?
- Why is the if-then block inside a forever loop?
- Which group of blocks has touching?
- Finish this rule: If it's cold outside, then...
Answers: 1. A yes-or-no question. 2. So the star keeps checking all the time, not just once at the start. 3. Sensing. 4. Anything sensible works, like "wear a jacket"!
What's Next?
You built a real game. Well done! But how many stars did you catch? Right now, nobody is counting. Next time you will learn about variables, which are like little boxes that remember things. You will add a score that goes up every time you catch the star, and maybe even a timer. Save your game so it's ready!
For Grown-Ups
Conditionals (if-then and if-then-else) are one of the core ideas in all programming, alongside sequences, loops and events from earlier episodes. The trickiest part for kids is usually understanding why the check must sit inside a forever loop: without it, the star asks "am I being touched?" only once, the moment the flag is clicked. If your child gets stuck, try acting it out: you be the star, and ask "Am I touched?" over and over while they reach for you. Please save the project, because Episode 6 adds a score to this exact game.