Conditions in Scratch
The “if” block and comparison operators let a program make decisions. We check whether a sprite touches an edge and build a simple logical decision.
All topics (9 of 11)
Chapter 1. Algorithm Basics
Chapter 2. Programming in Scratch
Chapter 3. Project Work
To make decisions in Scratch we use the “if ... then” block from the Control category. The blocks inside it run only when the condition is true. The “if ... then ... else” block runs other commands when the condition is false.
A condition is built from hexagonal blocks in the Sensing and Operators categories. For example, the “touching ...?” block checks whether a sprite touches another sprite or a colour, and the “>”, “<” and “=” blocks compare numbers. Several conditions are joined by the “and”, “or” and “not” blocks.
An example: let the cat turn back when it reaches the edge of the stage. After a “when green flag clicked” block, put “move 10 steps” and “if touching edge? then turn 180 degrees” inside a “forever” block. A small fix: to stop the cat from flipping upside down, set the rotation style to “left-right”.
Another example: if the sprite touches the colour red, it says “You lose!”. The script keeps checking the condition inside the “forever” loop. If the condition is checked only once, the event might be missed, so the check must go inside the loop.
Key takeaways
- Commands inside an “if” block run when the condition is true.
- The “if ... else” block gives two paths.
- A condition is built from sensing and operator blocks.
- The condition should be checked inside a “forever” loop.
Practice tasks
- Build a script in which the cat turns back at the edge of the stage.
- Write a script in which a sprite changes colour when the mouse pointer touches it.
- Build a script in which two sprites say “We crashed!” and stop when they touch.
Review questions
Test yourself: think first, then reveal the answer.
1When does an “if” block run the commands inside it?Show answerHide answer
Only when the condition is true.2Why should the condition go inside a loop?Show answerHide answer
So the program keeps checking it and does not miss the event.
0 of 11 topics completed
Sign in to mark