Programming Essentials in Scratch
Sequence, selection and iteration using blocks, plus variables and events. Everything you learn here transfers directly to Python.
Sequence
A sequence is a set of instructions carried out in order, one after another.
Computers are extremely literal. They do exactly what you say, in exactly the order you say it, which is why a program that seems obviously correct can still do something ridiculous.
when green flag clicked say "Hello!" for 2 seconds move 100 steps turn 90 degrees move 100 steps
Swap two of those blocks and the sprite ends up somewhere completely different.
Putting the right steps in the wrong order is just as wrong as having the wrong steps.
- Sequence: the blocks run one after another from the top, in the order they are stacked.
- Selection: the if block asks a question and picks one of two paths. Only one path runs.
- Iteration: repeat runs the blocks inside it again and again. Four sides, four identical steps, so repeat 4.
Selection and variables
Selection
Selection means the program chooses what to do based on a condition.
if <touching edge?> then turn 180 degrees
if <score > 10> then say "You win!" else say "Keep going"
The if ... else block gives two paths. Only one of them runs.
Conditions
A condition is a question with a yes or no answer:
touching edge?key space pressed?score > 10answer = "blue"
Variables
A variable is a named box that stores a value which can change while the program runs.
set score to 0 change score by 1 set lives to 3 change lives by -1
Common uses: score, lives, timer, player name, level number.
If you do not set score to 0 when the green flag is clicked, the score carries over from the last time you played, which is one of the most common bugs in Scratch projects.
Iteration and events
Iteration
Iteration means repeating instructions. Scratch has three loop blocks.
| Block | What it does |
|---|---|
repeat 10 | Runs exactly 10 times, a count controlled loop |
forever | Runs until the program is stopped |
repeat until <condition> | Runs until the condition becomes true |
Drawing a square:
repeat 4 move 100 steps turn 90 degrees
Four sides, four identical steps. Writing it out four times would work, but the loop is shorter, clearer and easier to change into a pentagon.
Events
An event starts a script when something happens.
when green flag clickedwhen space key pressedwhen this sprite clickedwhen I receive [message]
Broadcasting lets one sprite tell another sprite to do something, which is how sprites work together in a game.
Debugging
When a Scratch program does not work:
- Check the order of the blocks
- Check every variable is set at the start
- Check the condition is the right way round, for example
>where you meant< - Check the script is attached to the right sprite
- Click blocks individually to see what each one actually does
Step 1 of 3 Sequence: instructions run in order, top to bottom.
Key terms you must be able to define
- Algorithm
- A set of step by step instructions for solving a problem.
- Sequence
- Instructions carried out one after another in order.
- Selection
- Choosing what to do based on whether a condition is true.
- Iteration
- Repeating a set of instructions.
- Variable
- A named store for a value that can change while the program runs.
- Condition
- A question with a true or false answer, used to make a decision.
- Event
- Something that happens, such as a key press, that starts a script.
- Sprite
- An object in Scratch that can be moved, costumed and given its own scripts.
- Broadcast
- A message one sprite sends so that other sprites can respond.
- Debugging
- Finding and fixing errors in a program.
Flashcards
Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.
Reach the top grade on this topic
The top level at Key Stage 3 is about explaining choices, not just making things work.
Say which construct you used and why. "I used a repeat until loop rather than a repeat 10 loop, because the player might reach the target at any time and I do not know how many attempts they will need."
Predict before you run. Read a script and say what it will do before clicking the green flag. Being able to predict is what shows you understand it rather than having got there by trial and error.
Debug systematically. Do not just move blocks around until it works. Say what you expected, what actually happened, and what that difference tells you about where the fault is.
- Explain the difference between repeat, forever and repeat until, with an example use for each
- Predict the output of a script containing a loop and an if block
- Explain why a variable must be reset at the start of a game
- Find and describe a fault in a given script
Mistakes that cost marks here
- Forgetting to set variables back to their starting value when the green flag is clicked.
- Using forever when the loop needs to stop, so the rest of the script never runs.
- Attaching a script to the wrong sprite and then wondering why nothing happens.
- Putting the if block outside the loop when it needs to be checked on every repeat.
- Getting a condition the wrong way round, such as using less than when greater than was meant.
Scratch was made at MIT and released in 2007. It now has over 100 million registered projects, which makes it one of the largest collections of programs ever written by children anywhere in the world.
Knowledge check
repeat until <touching edge?> do?Exam-style questions
Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.
- LSequence is instructions carried out in order one after another
- LSelection is choosing what to do based on a condition
- LIteration is repeating instructions
Sequence means carrying out instructions one after another in the order they are written. Selection means the program chooses between different paths depending on whether a condition is true or false, using an if block. Iteration means repeating a set of instructions, either a fixed number of times or until a condition is met.
- LA variable stores a value that can change while the program runs
- LGives a valid example such as score, lives or a timer
- LExplains how it is used, such as increasing the score when a target is hit
A variable is a named store that holds a value which can change while the program is running, which is exactly what a game needs to keep track of what is happening. For example, a score variable would be set to 0 when the green flag is clicked, and then changed by 1 every time the player catches a falling object. The current value can then be checked in a condition, so the game can end or move to the next level when the score reaches a target.
- LVariables keep their value between runs
- LThe score is never set back to zero when the game starts
- LFix by adding a set score to 0 block after the green flag is clicked
The cause is that a variable keeps whatever value it was last given, and that value is remembered even after the program stops. Because the student's script never sets the score back to zero, the new game simply continues from the value left over at the end of the previous one. The fix is to add a set score to 0 block immediately after the when green flag clicked block, so that the score is reset every time a new game begins.
- LUses a repeat 4 block
- LContains a move block and a turn 90 degrees block
- LA loop means less code to write and read
- LIt is easier to change, for example altering the size or number of sides in one place
To draw a square you would use a repeat 4 block containing a move 100 steps block followed by a turn 90 degrees block. Each pass draws one side and turns the corner, and after four passes the sprite has turned through 360 degrees and returned to where it started. Using a loop is better than writing the two blocks out four times because the script is much shorter and easier to read, and more importantly it is far easier to change. If you wanted a larger square you would only have to edit one move block instead of four, and if you wanted a different shape you would only have to change the repeat number and the turn angle.
- LUses an if else block
- LCondition tests whether the score is 10 or more
- LCorrect message in each branch
if <score > 9> then say "Well done" else say "Keep trying" The if else block gives two paths and only one of them runs. The condition checks whether the score has reached 10, and because Scratch has a greater than block rather than a greater than or equal to block, testing for greater than 9 is the usual way to include 10 itself.