Selection means the program makes a choice. Instead of always doing the same thing, it looks at what is happening and picks what to do next.
The ‘if … then …’ block
The if block is shaped so other blocks fit inside it. It works like this: if something is true, then do the blocks inside. If it is not true, the inside blocks are skipped.
What is a condition?
A condition is a question the computer can answer with only true or false. For example, "Is the sprite touching the edge?" If the answer is true, the ‘if’ runs its inside blocks. If it is false, they are skipped.
| Condition | Answer | Run the inside blocks? |
|---|---|---|
| touching the edge? | true | Yes |
| touching the edge? | false | No, skip them |
A bouncing sprite
Often ‘if’ is checked again and again inside a loop, so the sprite keeps reacting. This is how a ball can bounce around the screen forever:
Put these steps in order for a bouncing sprite:
- Sprite moves 5 steps
- Check: is it touching the edge?
- If true, turn around
- Loop back and move again
The computer checks the condition every time round the loop, over and over, so the sprite keeps reacting to the edge without you doing anything more.