C3.1 Stage 3 Computing

Loops & repetition

Modify simple algorithms with loops & repetitive patterns (no conditionals yet); block programming (Scratch).

A loop is a way to tell a computer “do this again” without writing the same instruction over and over. It turns a long, repetitive list of steps into one short, tidy command.

What it means

An algorithm is just a list of instructions carried out in order — like a recipe or the steps to brush your teeth. In C1.1 you built sequences by writing each step out, one after another.

But some tasks repeat. To draw a square a turtle must: go forward, turn right, go forward, turn right, go forward, turn right, go forward, turn right. That’s eight steps, and four of them are identical pairs.

A loop captures the repetition. Instead of writing the pair four times, you say:

repeat 4 times: ( forward, turn right )

The number — here, 4 — is how many times the steps inside run. The steps inside are the body of the loop. The computer runs the body, counts one, runs it again, counts two, and stops when the count reaches the number you set.

Two ideas do all the work:

  • What repeats — the body.
  • How many times — the count.

Change the count and the pattern changes with it. This is powerful: repeat 100 times is exactly as short to write as repeat 4 times, but the computer does a hundred times the work. A loop is a small instruction that stands in for a large amount of doing.

Worked examples

1. Counting out loud. “Say a number, then add one, five times, starting at 0.”

repeat 5 times:
  say the number
  add 1

Output: 0, 1, 2, 3, 4. The loop ran 5 times, so 5 numbers came out.

2. Turtle square. A square has 4 equal sides and 4 equal corners (each a quarter-turn, 90°).

repeat 4 times:
  forward 100
  turn right 90

3. Change the count, change the shape. Keep the body but make the turn 360 ÷ sides:

repeatturn each timeshape
3120°triangle
490°square
660°hexagon

One loop draws any regular polygon — you only change two numbers.

The generative-art connection

Loops are where math becomes art you can watch build itself. In Scratch or Turtle Blocks, a single repeat block with a small turn and a tiny nudge each time sweeps a pen around into a spiral or a flower — the picture grows one iteration at a time, and the count is how many petals or points appear.

Our own dot-multiplier tool is a loop made visible: one dot is repeated around a circle into a symmetric starburst. Set the count to 6 and you get six dots evenly spaced; set it to 24 and the same rule fills the ring. You are not drawing 24 dots — you are writing one instruction and choosing how many times it runs. That is exactly a loop, and the symmetry you see is the count.

Common misconceptions

  • “The loop number is what I want to end at.” No — it’s how many times the body runs. repeat 5 starting from 0 gives 0,1,2,3,4, ending at 4, not 5.
  • “A loop is a special magic block.” It isn’t. repeat 3 times: (jump) is identical to writing jump, jump, jump. The loop is shorthand for the exact same steps — handy, not different.
  • “Everything inside runs, then repeats.” Only the steps inside the loop’s body repeat. A step written after the loop runs once, when the repeating is finished.

What a learner should be able to solve

The benchmark for mastering this concept — the problems a child at this stage is expected to be able to work through.

  1. Here is a repeat block that draws a shape: ``` repeat 5 times: forward 100 turn right 72 ``` A friend wants a shape with 8 equal sides instead. Which two numbers must change, and what should they become? (Remember: the turn each time is `360 ÷ number of sides`.)

    Answer

    Change the count `5` to `8`, and change the turn `72` to `360 ÷ 8 = 45`. So `repeat 8 times: forward 100, turn right 45`. It draws a regular octagon. (Check: the original `72` is `360 ÷ 5`, so the same rule gives the new turn.)

    Art hook A polygon slider: draw N dots evenly on a circle (angle step `360 ÷ N`) and connect them in order. A slider from N=3 to N=12 morphs triangle -> square -> ... -> dodecagon live, showing the loop count as the number of corners.

  2. Read this loop and write out exactly what it says, in order: ``` start at 2 repeat 4 times: say the number add 3 ``` How many numbers come out, and what are they?

    Answer

    `4` numbers come out (the body runs 4 times). They are `2, 5, 8, 11`: say 2 then add 3, say 5 then add 3, say 8 then add 3, say 11. The final `add 3` (to 14) still happens but is never said.

    Art hook A number-line stamper: place a dot at 2, then each loop step draw an arc jumping +3 to the right and stamp a labelled dot. The evenly spaced dots (2,5,8,11) make a visible rhythm; change the step size to re-space the whole pattern.

  3. Two loops are meant to do the same thing. Write the SHORT looped version of this long list: ``` hop, clap, hop, clap, hop, clap ```

    Answer

    `repeat 3 times: (hop, clap)`. The repeated unit is the pair `hop, clap`, and it appears 3 times, so the count is 3 and the body is the pair.

    Art hook A beat grid: each loop step lights one column of a row (hop = filled square, clap = ring). Repeating the 2-cell body across the grid makes a striped rhythm pattern; more repeats = a longer striped strip.

  4. A robot runs this program. What does it do, and how many times does it beep in total? ``` repeat 3 times: step forward beep step forward ``` Explain which steps repeat and which run only once.

    Answer

    The body (`step forward`, `beep`) repeats 3 times, so it beeps `3` times. The final `step forward` is OUTSIDE the loop, so it runs once after the looping finishes. Total: 4 steps forward, 3 beeps.

    Art hook A path plotter on a grid: each 'step forward' moves a dot one cell and draws a trail; each 'beep' drops a coloured marker. You see 3 markers inside the loop and one extra uncoloured step past them, making the 'outside the loop' step visible.

Training exercises

Practice problems, easier first, that build toward the bar above. Each doubles as a seed for an interactive artwork.

  1. How many times does the body of this loop run? ``` repeat 6 times: jump ```

    Answer

    `6` times. The count in front of a repeat block is exactly how many times the body runs, so `jump` happens 6 times.

    Art hook Place 6 dots evenly around a circle (one per loop run, angle step `360 ÷ 6 = 60`). Watch a dot appear each iteration until the ring of 6 is complete.

  2. Turn this long list into a loop: ``` clap, clap, clap, clap ```

    Answer

    `repeat 4 times: (clap)`. One repeated action, done 4 times.

    Art hook A tally-of-dots animation: each loop run stamps one dot in a row. Four claps = four dots. Slide the count up and the row grows in real time.

  3. This loop draws part of a shape: ``` repeat 3 times: forward 100 turn right 120 ``` What shape does it draw, why is the turn `120`, and what makes the shape close up neatly?

    Answer

    An equilateral triangle. A full turn around the outside is `360`, split over `3` corners, so each turn is `360 ÷ 3 = 120`. It closes because the 3 turns add up to exactly `360`, bringing the pen back to its start facing its start direction.

    Art hook Turtle triangle: animate a pen doing forward/turn 120 three times. Then let a slider change the count and auto-set the turn to `360 ÷ count`, morphing triangle into other polygons.

  4. Start at 0. Run: ``` repeat 5 times: say the number add 2 ``` Write the numbers that are said.

    Answer

    `0, 2, 4, 6, 8`. Five runs, saying then adding 2 each time. It ends by saying 8 (the last `add 2` to 10 is not said).

    Art hook Number-line hopper: a dot hops +2 each loop step, leaving a stamped dot at 0,2,4,6,8. The equal gaps show the 'skip counting' pattern the loop makes.

  5. Spot the mistake. A child wanted to draw a square (4 sides, quarter turns) and wrote: ``` repeat 4 times: forward 100 turn right 100 ``` The shape does not close up. What is wrong, and how do you fix it?

    Answer

    The turn is wrong. A square's corner turn is `360 ÷ 4 = 90`, not `100`. Change `turn right 100` to `turn right 90` and the square closes.

    Art hook Two turtles side by side: one turns 100 (a drifting near-square spiral), one turns 90 (a clean closed square). Seeing the gap that 100 leaves makes the 'must equal 360 ÷ sides' rule obvious.

  6. How many total actions happen when this runs? ``` repeat 4 times: wave spin ```

    Answer

    `8` actions. The body has 2 actions and runs 4 times, so `2 × 4 = 8`.

    Art hook A 4-by-2 grid where each loop run fills one row: column 1 = wave (blue square), column 2 = spin (spinning ring). The filled grid shows count × body = total cells.

  7. Which comes out the same as writing `hop, hop, hop`? Pick one and say why the other is wrong. A) `repeat 3 times: (hop)` B) `repeat 2 times: (hop, hop)`

    Answer

    A is correct: it does `hop` 3 times. B does `hop` `2 × 2 = 4` times, which is one hop too many.

    Art hook Show both loops as dot rows: A stamps a row of 3, B stamps a row of 4. Line them up under the target 'hop hop hop' to see which matches.

  8. Change ONLY the count so this loop draws a hexagon (6 sides). The turn already fits a hexagon. ``` repeat 3 times: forward 80 turn right 60 ```

    Answer

    Change the count `3` to `6`: `repeat 6 times: forward 80, turn right 60`. Six sides with 60 turns (`360 ÷ 6 = 60`) closes into a hexagon.

    Art hook A pen tracing the loop live: at count 3 it draws half a hexagon, and as you raise the count to 6 the shape finishes and closes. The count literally is how many sides appear.

  9. Careful counting! How many circles are stamped when this runs, and does the very last `move` stamp a circle? ``` repeat 4 times: stamp a circle move right move right ```

    Answer

    `4` circles are stamped: the body (`stamp a circle`, `move right`) runs 4 times, so there are 4 stamps. The extra `move right` at the bottom is OUTSIDE the loop, so it runs once at the end but stamps nothing — you get 4 circles and 5 moves.

    Art hook A row-stamper: each loop step drops a dot then shifts right one cell, making 4 evenly spaced dots; the final outside `move` slides the pen one more cell past the last dot, leaving a visible empty gap that shows the 'outside the loop' step.

  10. Open challenge: describe, in words or as a repeat block, the shortest loop that would stamp 12 dots evenly spaced around a circle. What is the turn between dots?

    Answer

    `repeat 12 times: (stamp a dot, turn right 30)`. Twelve evenly spaced dots means each turn is `360 ÷ 12 = 30`. Only the count and the turn change to get any number of evenly spaced dots.

    Art hook A clock-face maker: the loop stamps 12 dots at 30 apart, exactly like clock hours. Change the count to 5, 8 or 24 and the ring re-spaces itself — a dot-multiplier where the loop count sets the symmetry.