AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

The repeat block

Week 9 · Unit 4: Be a storyteller · 3 lessons
2

From algorithm to Scratch

Computing 4: Unit 4, pp. 48–61
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.

AlgorithmScratch
1 Startwhen green flag clicked
2 Move 15 stepsmove (15) steps (inside the loop)
3 Wait half a secondwait (0.5) seconds (inside the loop)
4 Repeat steps 2 and 3 three more timesrepeat (4)
5 Stopthe script ends
Worked examples
whenclickedmove15stepswait0.5secondsrepeat4
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.
whenclickedset size to30%change size by20wait0.5secondsrepeat3
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 %.
whenclickedset size to30%change size by20wait0.5secondsrepeat3
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?
StepInstruction
1Start
2Turn 90 degrees
3Wait 1 second
4Repeat steps 2 and 3 two more times
5Stop
2
The algorithm says “Say Hello for 1 second” and then “Repeat step 2 four more times”. Is this script right? Fix it.
whenclickedsayHello!for1secondsrepeat4
3
In the balloon algorithm, why does “set size to 30 %” stay outside the loop?
4
Trace the script. What is the size at the end and how many seconds does it take?
whenclickedset size to100%change size by-10wait1secondsrepeat3