Loops: Repeat, Repeat, Repeat!

Loops: Repeat, Repeat, Repeat!: Block Coding lesson banner with Beep and Pixel

Track: Block Coding  ·  Lesson 3 of 10  ·  Ages: 6+  ·  Time: 35 minutes

Pixel the cat loves to knead a blanket: push, push, push, push. That is the same move again and again. Programmers have a name for doing the same thing many times. It is called a loop, and it is one of the most powerful ideas in coding.

Pixel says If you ever drag the same blocks over and over, stop! There is almost always a shortcut. Meow!

What you need

  • A block-coding app that a grown-up has helped you open.
  • A pen extension or pen blocks, if your app has them. Many apps keep the pen blocks in an “add more blocks” menu.
  • About 35 minutes.

Grown-ups Loops are where children first feel the power of programming: four lines become one. If your app hides its pen blocks behind an extension button, you may need to help find it. If the pen is not available, the dance activity works without it.

The long way to draw a square

A pen block lets a character draw a line behind it as it moves. With the pen down, Beep leaves a trail. Here is a square, made the long way:

A long script: pen down, then move 100 steps and turn right 90 degrees, four times in a row.

Do you spot the pattern? Move 100, turn 90 shows up four times. A square has 4 sides and 4 corners, so that makes sense.

The short way: a repeat loop

A repeat block holds other blocks inside its C-shaped mouth, and it runs them again and again. You choose how many times. The blocks inside the C are the ones that repeat.

A script with pen down, then a repeat 4 block that holds move 100 steps and turn right 90 degrees.

This does the very same job with fewer blocks! Beep walks 100 steps, turns right a quarter turn, and does it four times. Beep ends up back where it started and has drawn a square.

Beep says Why 90? One whole turn all the way round is 360 degrees. A square has 4 corners, and 4 turns of 90 make 90 + 90 + 90 + 90 = 360. After that, I am facing the way I started. Beep boop!

Forever

A forever block is a loop that never stops by itself. It keeps going until you press the stop button. This script makes Beep spin slowly:

A script with a forever loop that turns right 5 degrees and then waits 0.1 seconds, again and again.

Beep turns 5 degrees every tenth of a second. A full spin is 360 degrees, and 360 ÷ 5 = 72 little turns. So Beep spins all the way around about every 7 seconds, since 72 turns at 0.1 seconds each is 7.2 seconds.

A wiggle dance

A loop does not have to be forever. This dance repeats 8 times and then stops:

A script with a repeat 8 loop holding turn right 15, wait 0.2, turn left 15, wait 0.2, followed by a say Ta-da block after the loop.

Notice the say block is below the loop, not inside it. It only happens after all 8 wiggles are done. Inside the loop is “again and again”. Below the loop is “when that is finished”.

Same idea in Python

Real programmers write loops in text too. This Python program does the same job as the square script. Python’s turtle is like a pen robot on the screen.

import turtle

t = turtle.Turtle()
for side in range(4):
    t.forward(100)
    t.right(90)

turtle.done()
The drawing made by the turtle code above
This is what the code above draws.

for side in range(4): means “do the lines below 4 times”, just like the repeat 4 block. The pushed-in lines are the ones inside the loop, just like the blocks inside the C.

Challenges

Try it Change the square into a triangle. A triangle has 3 sides. How many degrees should Beep turn at each corner?

Show a hint

A full trip round is 360 degrees. Share it between the 3 corners: 360 ÷ 3 = ?

Show the answer

Beep should turn 120 degrees at each corner, and repeat 3 times.

A script with pen down and a repeat 3 loop holding move 100 steps and turn right 120 degrees.

Try it Pixel built a square script but forgot the turn block inside the loop: repeat 4, move 100 steps. What do you think it draws?

Show the answer

It draws a straight line that is 400 steps long. Beep walks 100 steps four times in the same direction, because it never turns.

Try it What happens if you change repeat 4 to repeat 8 in the square script but leave the 90-degree turn? Make a guess first, then test it!

Show the answer

You still see just one square. With 8 repeats, Beep turns 8 × 90 = 720 degrees, which is two whole turns. It draws the same square twice, right on top of itself.

Did you know? Loops are everywhere. Your heart beats in a loop, the days of the week go round in a loop, and a video game checks if you pressed a button in a loop many times every second.

What you learned

  • A loop repeats blocks so you do not have to drag them again and again.
  • A repeat block runs the blocks inside it a set number of times.
  • A forever block keeps going until you press stop.
  • To draw a square, repeat 4 times: move, then turn 90 degrees.
  • Blocks inside the loop repeat. Blocks below the loop happen after it is finished.