Making Decisions with Conditions
💻 ScratchUse if, if-else and comparison blocks to control what happens next.
A condition is a question the program asks. If the answer is true, one set of blocks runs. If it is false, another set can run instead.
- Use if and if-else blocks.
- Compare values with operators.
- Combine conditions with and/or.
If and If-Else
The if block runs its contents only when the condition is true. The if-else block chooses between two paths.
Comparison Operators
Scratch compares numbers and text with =, < and >. Use them to check the score, time or position.
when green flag clicked
forever
if <(timer) > (30)> then
say [Time is up!]
stop [all v]
end
end
if <(score) > (10)> then
say [Great job!]
else
say [Keep trying!]
endCombining Conditions
Use and when both conditions must be true, and or when either one is enough. not reverses a condition.
Practice
- Make the sprite say "Hot" if the temperature variable is above 35.
- Add an else branch that says "Cool" otherwise.
- End the game when lives reach zero.
Hands-on exercise
Add a lives system.
1. Set lives to 3 at the start.
2. Subtract one life when the sprite touches the danger sprite.
3. If lives is less than 1, say "Game over" and stop all scripts.
Recap
Conditions let the program choose. Use if for one path and if-else for two, and combine tests with and, or and not.
Example
when green flag clicked
forever
if <(timer) > (30)> then
say [Time is up!]
stop [all v]
end
end
if <(score) > (10)> then
say [Great job!]
else
say [Keep trying!]
end
Exercises
1. Set lives to 3 at the start.
2. Subtract one life when the sprite touches the danger sprite.
3. If lives is less than 1, say "Game over" and stop all scripts.
Expected Output
The game stops and displays Game over when lives reach zero.
Lesson Nav
Scratch Course
0/9 lessons completed
- 1 Your First Scratch Project Concept
- 2 Motion and Looks Blocks Tutorial
- 3 Events and Repeat Loops Tutorial
- 4 Your First Scratch Game Project
- 5 Variables and Keeping Score Tutorial
- 6 Making Decisions with Conditions Tutorial
- 7 Sensing, Input and Operators Tutorial
- 8 Clones, Timers and Levels Tutorial
- 9 Project: Build a Story Game Project