Conditions
Checking sprite attributes using if-then nodes
Definition
Conditions are control structures (If-Then, If-Then-Else) that execute code paths only when specified criteria (sensing touch, comparisons) are true. Key concepts include Hat Block.
Think of Conditions as:
Just as an actor changes costume or walks across the theater grid on cue, Conditions dictates sprite behavior.
Real-Life Example
Just as you coordinate actions in real life, Conditions works by structuring logic commands in sequence:
- 1 Register the trigger event block.
- 2 Run calculations or movements within a continuous control block.
- 3 Update values or graphics on the stage viewport.
Key Highlights:
- Trigger event
- Logic evaluation
- Visual update
Interactive Diagram
Launch the interactive diagram to see this in action.
Open Interactive DiagramThe interactive diagram for this chapter demonstrates Conditions. It shows if-then and if-then-else blocks with conditions that change the flow of a program.
What to explore:
- change the condition value; watch different branches execute; see the program choose a path based on the condition
- conditional blocks let programs make decisions — different code runs depending on whether a condition is true or false
Introduction
Life is full of decisions. If it is raining, you take an umbrella. If you are hungry, you eat. If a traffic light is red, you stop. Computers make decisions too, using a programming concept called Conditions. Conditions let your program choose different paths based on different situations. Without Conditions, your program would do the same thing every time, regardless of what is happening.
Conditions use true/false questions called Boolean expressions. Is the score greater than 10? Is this sprite touching that sprite? Is the space bar pressed? Each question evaluates to either true or false. Depending on the answer, the program can run different blocks. Conditions are what make games interactive — they determine when you score, when you lose, when an enemy chases you, and when you level up.
In this chapter, you will learn how to use 'if' and 'if-else' blocks, understand Boolean expressions, combine Conditions with 'and', 'or', and 'not', and use Conditions to create intelligent behaviors in your projects. By the end, you will be able to make sprites that react differently in different situations.
How It Works
The 'if () then' block checks a condition. If the condition is true, it runs the blocks inside its C-shaped body. If the condition is false, it skips them. For example: 'if
The 'if () then [else]' block extends the basic 'if' with an alternative. If the condition is true, the first set of blocks runs. If false, the blocks in the 'else' section run. For example: 'if
Household Object Analogy
Think of Conditions like a fork in the road. When you reach a fork, you check a condition: 'Do I need to go to school today?' If yes, you take the left path (to school). If no, you take the right path (to the park). The 'if' block is like a simple fork with one path (go to school or stay home). The 'if-else' block is a fork where both paths lead somewhere different.
Deeper Dive
Boolean expressions can be combined using 'and', 'or', and 'not' operators (in the Operators category). 'And' requires all Conditions to be true. 'Or' requires at least one condition to be true. 'Not' reverses the result (true becomes false, false becomes true). For example: 'if <
Comparison operators — greater than (>), less than (<), and equals (=) — are used to compare values. These are also in the Operators category. You can compare numbers (score > 10), variables (lives = 0), or sensor values (loudness > 50). These comparisons produce Boolean values (true/false) that can be used as Conditions in 'if' blocks.
Conditions are most powerful when combined with loops. A common pattern is the game loop: a 'forever' loop containing multiple 'if' blocks that check for different Conditions each frame. One 'if' checks for key presses, another checks for collisions, another checks for score thresholds. This pattern — read inputs, update state, check rules — is the foundation of almost every game.
Key Insight
The condition in an 'if' block is checked only at the moment the block is reached. If the condition changes later, the 'if' block does not re-check — it already made its decision. This is why Conditions are typically placed inside loops, so they are checked repeatedly and can respond to changing situations.
Advanced
Nested Conditions — an 'if' block inside another 'if' block — allow for multi-level decision making. For example, 'if
The '
Conditions are related to Boolean logic created by George Boole in the 19th century. Boolean algebra is the foundation of digital circuit design and all computer decision-making. Every 'if' block you use in Scratch ultimately relies on the same logical principles that govern computer processors, search engines, and artificial intelligence systems.
Vocabulary Table
| Term | Definition |
|---|---|
| Conditions | The primary technological concept explaining how components interact within the context of Scratch Programming. |
| Coordinate Grid | The X and Y plane representing the Stage layout coordinates. |
| Hat Block | A block with a rounded top that registers events to launch scripts. |
| Sensing Block | A block that checks for collisions, touch parameters, or mouse pointer coordinates. |
Fun Facts
The 'if/else' block was added in Scratch 1.3. Before that, programmers had to use two separate 'if' blocks (one for true, one for false) to achieve the same effect.
Boolean values are named after George Boole, a 19th-century mathematician who developed Boolean algebra. His work is fundamental to all modern computing.
The '
Conditions can be chained: 'if (condition1) then [if (condition2) then [action]]' creates a logical AND without using the 'and' operator.
The '
Common Misconceptions
Misconception: An 'if' block can only check one condition.
Truth: An 'if' block can check complex Conditions using 'and', 'or', and 'not' operators. You can combine multiple Conditions into a single check.
Misconception: The 'else' part of an 'if-else' block is required.
Truth: The 'else' branch is optional. Use 'if' alone when you only need to do something when the condition is true, and nothing when it is false.
Misconception: Conditions can only check user input.
Truth: Conditions can check anything: sprite positions, variable values, timers, sensor readings, colors, touch detection, and more.
Misconception: If you use 'if-else', exactly one branch always runs.
Truth: Correct — 'if-else' is an either-or structure. Exactly one of the two branches executes, never both, never neither.
Knowledge Check
1. What is the main role of Conditions?
Answer: To orchestrate behaviors and controls visually
2. Which block shape starts a script stack in Scratch?
Answer: Hat block (curved top)
3. What is the maximum X coordinate limit on the stage?
Answer: 240
