GoalUse the repeat block in Scratch to run blocks a set number of times.
New words
repeat block · a Scratch block that runs the blocks inside it a set number of timesnumber box · the white oval where you type a numberrun · to carry out the blocks of a script
Explanation
The repeat block is in the Control group. It has a number box and a mouth. Type a number in the box and the blocks inside the mouth run that many times. When the last trip is done, the script carries on with the blocks underneath. This is the big difference from the forever block, which never lets the script carry on. The repeat block is how we make a count-controlled loop in Scratch. If the loop holds the block “move (10) steps” and the number is 7, the sprite moves 70 steps in all. Change the number and you change how many times the loop runs.
Worked examples
A sprite starts at x: 0. The loop moves it 10 steps five times, so it ends at x: 50. Then the script is finished.
A snail starts at x: −100. It moves 20 steps and waits half a second, six times. The six moves add up to 120 steps, so it ends at x: 20. The loop takes 6 × 0.5 = 3 seconds.
Compare the two loops. They hold the same blocks, but the repeat loop stops after six trips and the forever loop never stops.
Class activity
Be the repeat block (with a friend): write a number on a card, such as 6. You are the repeat block and hold the card. Your friend is the sprite and hops once each time you say “go”. Count the hops out loud and stop when the count matches the card. Now say “Done!” once. Change the card to 3 and guess how many hops there will be before you start. Try a loop with two actions: hop and clap.
Practice
1
How many times does the sprite say “Hi!” and how many seconds does the loop take?
3 times, and 3 seconds.
2
A sprite starts at x: 20. What is its x position at the end?
x: 80 (4 × 15 = 60, and 20 + 60 = 80).
3
Why is repeat (8) better than writing “move (10) steps” eight times?
It is shorter and easy to change: you only change one number.
4
Which script stops by itself? How far does it turn in all?