GoalTurn an algorithm that says “more times” into a Scratch script with a repeat block.
New words
algorithm · a set of steps to do a jobhat block · the block that starts a scriptconvert · to change something from one form into another
Explanation
We can convert an algorithm into a Scratch script, step by step. Step 1, “Start”, becomes the hat block. The steps that repeat go inside the mouth of a repeat block. Steps that happen only once stay outside the loop, either before it or after it. “Stop” just means the script ends. Be careful with the number. The algorithm says “repeat steps 2 and 3 three more times”. Those steps already ran once, so the loop must run 1 + 3 = 4 times, and the repeat block gets the number 4. The number in the block is always the total.
Algorithm
Scratch
1 Start
when green flag clicked
2 Move 15 steps
move (15) steps (inside the loop)
3 Wait half a second
wait (0.5) seconds (inside the loop)
4 Repeat steps 2 and 3 three more times
repeat (4)
5 Stop
the script ends
Worked examples
This is the script for the algorithm in the table. The sprite moves 15 steps and waits half a second, four times, so it moves 60 steps in 2 seconds.
A balloon algorithm: 1 Start. 2 Set size to 30 %. 3 Change size by 20. 4 Wait half a second. 5 Repeat steps 3 and 4 two more times. 6 Stop. Step 2 happens once, so it stays above the loop. The loop runs 1 + 2 = 3 times, and the size grows 3 × 20 = 60, from 30 % to 90 %.
A common mistake: the “set size” block is put inside the loop. Each trip sets the size back to 30 %, and then adds 20, so the balloon stays at 50 % and never grows any bigger.
Class activity
Paper blocks (with a friend): write these steps on separate paper strips: Start, Clap, Stamp, Repeat steps 2 and 3 two more times, Stop. Now make the Scratch version on a new set of strips. Use a green-flag strip, a strip that says “repeat” with a number, and one strip each for Clap and Stamp. Place the Clap and Stamp strips inside a folded paper “mouth” under the repeat strip. Which number goes in the box? How many claps and stamps will there be?
Practice
1
Which number goes in the repeat block for this algorithm?
Step
Instruction
1
Start
2
Turn 90 degrees
3
Wait 1 second
4
Repeat steps 2 and 3 two more times
5
Stop
3 (the steps run once and two more times).
2
The algorithm says “Say Hello for 1 second” and then “Repeat step 2 four more times”. Is this script right? Fix it.
No. The loop runs 1 + 4 = 5 times, so it should be repeat (5).
3
In the balloon algorithm, why does “set size to 30 %” stay outside the loop?
It must happen only once, at the start. Inside the loop it would reset the size every time.
4
Trace the script. What is the size at the end and how many seconds does it take?