Introduction to 'Repeat' blocks to help students write shorter and more efficient code.
Imagine you are a robot tasked with drawing 100 squares on a giant playground. Would you rather write the instruction 'Move Forward' 400 times, or just tell the robot to 'Repeat' a few simple steps?
In coding, we often want our characters (called sprites) to do the same thing over and over. Think about walking: you lift your left foot, then your right foot, then your left again. This is a pattern. If you want to draw a square, you move forward and turn right. You do this exactly 4 times. Instead of dragging 8 different blocks into your workspace, you can use a loop to tell the computer to do that pattern for you!
Quick Check
If a dance move has the same three steps repeated over and over, what do we call that sequence?
Answer
A pattern.
To draw a square without a loop, you need many blocks. With a loop, it is simple: 1. Grab a Repeat block. 2. Set the number to . 3. Inside the loop, put a 'Move 100 steps' block. 4. Also inside, put a 'Turn Right 90 degrees' block.
The computer will do these two steps times, creating a perfect square!
The Repeat block is like a hungry mouth that eats other blocks! Anything you put inside the mouth of the repeat block will happen again and again. The number you type into the block tells the computer exactly how many times to run that code. If you want a sprite to blink times, you just put the 'Hide' and 'Show' blocks inside a repeat block set to . This makes your code much cleaner and easier to read.
Quick Check
If you put a 'Jump' block inside a repeat block set to 5, how many times will your sprite jump?
Answer
5 times.
Let's make a ball bounce up and down 10 times. 1. Drag out a Repeat block and set it to . 2. Inside, add 'Change Y by 50' (this moves it up). 3. Add 'Wait 0.5 seconds'. 4. Add 'Change Y by -50' (this moves it down).
Because movement actions are happening, your code stays short even though the ball moves a lot!
Using loops is a sign of a great programmer. We call this being efficient. Loops are better for three reasons: 1. Speed: It is much faster to change one number than to delete 50 blocks. 2. Accuracy: You are less likely to make a mistake if you only write the code once. 3. Readability: Other people can understand your code quickly. If you see a 'Repeat 100' block, you know exactly what is happening without counting 100 individual lines!
Imagine you want to draw a staircase with 20 steps. Each step is pixels high and pixels wide. 1. Use a Repeat block set to . 2. Inside, put: 'Move 10 steps', 'Turn Left 90', 'Move 10 steps', 'Turn Right 90'. 3. Total distance moved horizontally will be pixels. 4. Total distance moved vertically will be pixels.
Which block would you use to make a sprite do the same action 10 times?
If you want to draw a triangle (3 sides), what number should you put in the Repeat block?
Using a loop makes your code longer and harder to read.
Review Tomorrow
Tomorrow, try to explain to a friend or parent why a 'Repeat 4' block is better than dragging out the same block four times.
Practice Activity
Open your coding tool and try to make a sprite draw a hexagon (6 sides) using a single Repeat block!