Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Loops and patterns

Learn how a loop repeats steps, how to spot patterns, and how to write long instructions in a short way.

Get the whole book: Computers and coding for classes 6 to 8 (8 lessons, free to print)

Let us begin

The big idea

Clap, stomp, clap, stomp, clap, stomp. Writing that out is slow, but we can say repeat 3 times: clap, stomp. This short way of repeating is called a loop. Programmers use loops all the time.

A loop repeats a set of steps again and again. Instead of writing the same steps many times, we write them once and say how many times to repeat.

A pattern is something that repeats in a regular way, like red, blue, red, blue. Loops and patterns go together: if you can see the pattern, you can write a loop for it.

We can draw a square with a loop. Repeat 4 times: walk forward, turn right 90 degrees. The same two steps happen four times, so the loop is short.

A loop must know when to stop. A loop that never stops is called an endless loop, and the computer would be stuck repeating forever.

Number patterns work like loops too. In 3, 6, 9, 12, we start at 3 and add 3 each time.

A loop that repeats clap and stompA large box labelled Repeat 3 times contains two smaller boxes, Clap and Stomp. An arrow goes from Clap to Stomp, and a return arrow goes underneath from Stomp back to Clap. Repeat 3 times Clap Stomp Result: clap, stomp, clap, stomp, clap, stomp
The loop goes round and round three times.

Worked examples

See how it works

  1. Counting actions

    Repeat 4 times: clap, clap, stomp. In one round there are 2 claps and 1 stomp, which is 3 actions. After 4 rounds: 4 x 2 = 8 claps, 4 x 1 = 4 stomps, and 4 x 3 = 12 actions altogether.

  2. Adding in a loop

    Start with total = 0. For each number from 1 to 5, add it to the total. 0+1 = 1, 1+2 = 3, 3+3 = 6, 6+4 = 10, 10+5 = 15. The final total is 15.

  3. A number pattern

    The pattern is 3, 6, 9, 12. Each step adds 3, so the loop is: start at 3, add 3 each time. The next number is 12 + 3 = 15.

  4. Drawing a square

    Repeat 4 times: forward 3 steps, turn right 90 degrees. Walking 3 steps four times is 12 steps in all. You end up where you started, having walked a square.

Try it at home

Loop dance

You will need

  • paper and pencil
  • an open floor space

What to do

  1. Write a dance of 3 moves, such as clap, jump, spin.
  2. Write it as a loop: repeat 4 times: clap, jump, spin.
  3. Do the dance and count how many moves you did in all.
  4. Change the loop to repeat 2 times and see how the dance changes.
  5. Now draw a pattern of shapes with pencil and write the loop that makes it.

Ask a grown-up: Make sure the floor space is clear. Jumping and spinning are done at a gentle level, and nobody should feel hurried.

Fun fact

A loop saves effort: a computer can repeat a step a million times without getting bored or tired.

Practice sheet

Check what you learned

Write your answers in a notebook, or print this sheet and write on it. When you finish, mark your work with the answer key.

Practice sheet: Loops and patterns (Classes 6-8, Computers and coding)

  1. In one sentence, what is a loop?
  2. Write jump, jump, jump, jump, jump using a loop.
  3. Repeat 4 times: clap, stomp. How many claps are there?
  4. Repeat 3 times: clap, clap, stomp. How many actions are there in all?
  5. What comes next: red, blue, red, blue, red, ?
  6. Which loop draws a square?
  7. Repeat 4 times: forward 3 steps, turn right. How many steps are walked in all?
  8. Start with 2. Repeat 5 times: add 4. What is the final number?
  9. To draw an equilateral triangle with repeat 3 times, how many degrees should you turn each time?
  10. Find the next two numbers: 1, 2, 4, 8, ?, ?

Answer key

Answers: Loops and patterns

Do the practice sheet first, then check here. If an answer is different from yours, read the lesson again and try once more.

  1. A loop repeats a set of steps again and again.
  2. Repeat 5 times: jump.
  3. 4 claps (and 4 stomps).
  4. 3 x 3 = 9 actions.
  5. blue (and then red).
  6. Repeat 4 times: forward, turn right 90 degrees.
  7. 4 x 3 = 12 steps.
  8. 2 + 4 x 5 = 2 + 20 = 22.
  9. 120 degrees.
  10. 16 and 32 (each number doubles).
WhatsApp us