Find a course

Module 2: Loops and ChoicesLesson 4 of 79 min

Loops: Repeat It!

Why write the same step ten times? Learn how loops repeat steps, find patterns in dances and drawings, and guide a robot with fewer commands.

Step 1 of 13Learn

Lazy can be smart

Imagine telling a robot to walk to the door:

Step forward. Step forward. Step forward. Step forward. Step forward. Step forward.

That is a lot of writing! A smarter way is a loop:

Repeat 6 times: step forward.

A loop repeats one or more steps again and again. It makes algorithms shorter, easier to read and easier to fix. If the door moves farther away, you change one number instead of adding lines.

Step 2 of 13Practice · Choose one

Which is the shortest way to tell a robot to jump 10 times?

Step 3 of 13Learn

Loops with more than one step

A loop can repeat a group of steps. Look at this dance:

Repeat 3 times: clap, stomp, spin.

That makes: clap, stomp, spin, clap, stomp, spin, clap, stomp, spin. That is 9 moves from one short line!

To count the moves, multiply: steps inside the loop times the number of repeats. Here 3 steps times 3 repeats is 9.

Step 4 of 13Practice · Work it out

A loop says: Repeat 4 times: hop, hop, wave. How many moves is that in total?

Step 5 of 13Practice · Chart

Repeat: clap, stomp, spin

Each repeat adds 3 more moves.

Tap the bar that has 12 moves.

Tap the bar that answers the question, then press Check.

Repeat: clap, stomp, spinBar chart with 5 categories: 1 time: 3; 2 times: 6; 3 times: 9; 4 times: 12; 5 times: 15.051015Moves1 time2 times3 times4 times5 times3691215
Repeat: clap, stomp, spin
ItemTotal moves
1 time3
2 times6
3 times9
4 times12
5 times15

Step 6 of 13Try the calculator

Loop counter

Change the numbers to see how many moves one loop makes.

Total moves
15

Step 7 of 13Learn

Patterns hide loops

Programmers look for patterns, things that repeat. When you see a pattern, you can use a loop.

To draw a square, a robot can do: forward 10, turn right, forward 10, turn right, forward 10, turn right, forward 10, turn right.

See the pattern? It is Repeat 4 times: forward 10, turn right. A square has 4 equal sides and 4 turns, so the loop runs 4 times.

Step 8 of 13Practice · Choose one

A robot runs Repeat 3 times: forward 10, turn. What shape does it draw if each turn is the right size?

Step 9 of 13Practice · Spot the mistake

This loop is supposed to draw a square. Tap the buggy part.

Step 10 of 13Compare

Loops that draw shapes

AspectTriangleSquareHexagon
Repeat3 times4 times6 times
Inside the loopForward 10, turnForward 10, turnForward 10, turn
Size of each turn120 degrees90 degrees60 degrees

Step 11 of 13Note

Step 12 of 13Practice · Code challenge

Make claps(n) return a word with n claps in a row. claps(3) should return "clapclapclap". Right now the loop always runs 2 times. Change it so it runs n times.

Runs in a sandbox in your browser.
  • claps(3)
  • claps(1)
  • claps(5)

Step 13 of 13Practice · Fill in the blanks

Fill in the blanks

A part of an algorithm that repeats steps is called a .

Word bank