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

Dance routines

Week 10 · Unit 4: Be a designer · 3 lessons
2

Repeat blocks

Computing 3: Unit 4, pp. 60–78
GoalUse a repeat block to replace blocks that repeat in the same order, and trace what the program does.
New words
repeat block · a block that runs the blocks inside it again and againloop · a part of a program that repeatsinside · between the arms of the repeat block
Explanation

Look at a dance with the same two moves again and again: right, left, right, left, right, left. We can write each move as a block, or we can use a repeat block and put the two moves inside it, with the number of times. The repeat block counts as one block, so the short program has far fewer blocks. Take care: only blocks that must happen again go inside the repeat. A block that should happen once, such as a go to block that resets the sprite, stays outside, before the repeat.

Long (7 blocks)whenspacekey pressedchange x by20change x by-20change x by20change x by-20change x by20change x by-20Short (4 blocks)whenspacekey pressedchange x by20change x by-20repeat3
Worked examples
Start x: 0Long programShort program
after loop 120, then 020, then 0
after loop 220, then 020, then 0
after loop 320, then 020, then 0
In both programs the sprite moves 20 right and 20 left three times. It ends where it started. The long program has 7 blocks, and the short one has 4 (the hat, the repeat and the two blocks inside).
whenspacekey pressedset size to100%change size by10wait1secondsrepeat4
This program grows a sprite and waits, 4 times. Start with size 100. After each loop the size goes up by 10: 110, 120, 130, 140. Each loop waits 1 second, so the program takes 4 seconds.
whenspacekey pressedgo to x:0y:0move10stepsrepeat3
A common mistake is putting the reset inside the repeat. Here the go to block puts the sprite back to x: 0 each time, so the sprite always ends at x: 10, not at x: 30. The reset belongs before the repeat.
Class activity

Repeat in real life (with a friend): choose a pattern of three moves, such as clap, stamp, turn. Do it 4 times without stopping. Then write it as a program: “repeat 4 times: clap, stamp, turn”. Count the blocks for the long version and the short version. How many blocks does the repeat save?

Practice
1
Count the blocks in the long program and in the short program in the explanation above.
2
A sprite starts at size 100. What is its size at the end, and how long does the program take?
whenspacekey pressedset size to100%change size by-10wait1secondsrepeat5
3
Write a short program for: move 10 steps, turn ↻ 90 degrees, four times. Where does the sprite end?
4
This sprite should grow to size 180, but it always ends at size 120. Why? How can we fix it?
whenspacekey pressedset size to100%change size by20repeat4