New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Scratch› Making Decisions with Conditions

Making Decisions with Conditions

💻 Scratch

Use if, if-else and comparison blocks to control what happens next.

Lesson 6 of 9 Tutorial
0 of 9 completed

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.

By the end of this lesson you can:
  • 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!]
end

Combining Conditions

Use and when both conditions must be true, and or when either one is enough. not reverses a condition.

Warning Place the condition inside the forever loop if it must be checked continuously. A single if runs only once.

Practice

  1. Make the sprite say "Hot" if the temperature variable is above 35.
  2. Add an else branch that says "Cool" otherwise.
  3. 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

Exercise 1. Lives and game over Medium
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.
Scratch exercise-1.txt
Expected Output
The game stops and displays Game over when lives reach zero.

Lesson Nav

Lesson sections