Repetition and Loops

⏱ 9 min✏️ Quiz at the end

Do It Again (and Again)

Computers never get bored. Ask one to do something a million times and it will β€” perfectly, every time. Repetition (also called iteration or looping) is the building block that makes this possible.

Instead of writing:

draw a star
draw a star
draw a star
draw a star
draw a star

we write:

REPEAT 5 TIMES:
    draw a star

Shorter, clearer, and if you need 500 stars instead of 5, you change one number instead of copying 495 more lines.

Two Kinds of Loop

Count-controlled β€” "repeat N times"

Use when you know in advance how many repeats you need:

REPEAT 10 TIMES:
    do a star jump

The loop keeps its own count and stops after exactly 10 rounds.

Condition-controlled β€” "repeat until…"

Use when the number of repeats depends on what happens:

REPEAT UNTIL the water boils:
    keep heating

You cannot know beforehand how many rounds this takes β€” the loop checks its condition (the same true/false tests from selection) and stops when it becomes true.

Count-controlledCondition-controlled
Repeatsa fixed number of timesuntil a condition is met
Use whenthe count is known in advancethe count depends on events
Exampleprint 20 ticketskeep guessing until correct

Loops in Flowcharts

In a flowchart, a loop is an arrow that goes back up:

        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”Œβ”€β”€β”€β–Ίβ”‚  Heat the    β”‚
   β”‚    β”‚  water       β”‚
   β”‚    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚           β–Ό
   β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚ NO β”‚   Boiling?   β”‚
   └────│              β”‚
        β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚ YES
               β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚  Make tea    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Each pass: heat, then test. If not boiling, loop back; if boiling, continue on.

The Infinite Loop Trap

A loop must be able to stop. If its exit condition can never become true, the loop runs forever β€” an infinite loop, one of the most famous bugs in computing.

Trace this, starting with number = 3:

REPEAT UNTIL number > 10:
    subtract 1 from number

3 β†’ 2 β†’ 1 β†’ 0 β†’ βˆ’1 β†’ … The number only shrinks, so number > 10 can never be true. The loop never ends and the program freezes.

The fix: make sure something inside the loop moves the condition toward true β€” here, add 1 instead of subtracting. When debugging a frozen program, a broken loop condition is a prime suspect.

Loops + Patterns = Power

Repetition is where pattern recognition pays off. Spot that all 20 snowmen are drawn the same way, and the whole job becomes:

REPEAT 20 TIMES:
    draw body
    draw head
    add eyes, nose, hat

Three building blocks β€” sequence, selection, repetition β€” and you can now describe astonishingly complex behaviour.

Worked Example β€” A Guessing Game Loop

Condition-controlled loops shine brightest when the number of repeats is genuinely unpredictable. Trace this number-guessing game, where the secret number is 7:

guess ← 0
attempts ← 0
REPEAT UNTIL guess = 7
    INPUT guess
    attempts ← attempts + 1
ENDREPEAT
OUTPUT "You got it in ", attempts, " tries!"
Attemptguess enteredguess = 7?attempts
13No1
29No2
37Yes3

The loop repeats exactly as many times as the player needs β€” three tries here, but it could just as easily be one or fifteen. That unpredictability is the whole point of choosing a condition-controlled loop over a count-controlled one: nobody could have written REPEAT 3 TIMES in advance, because nobody knew how many guesses this particular game would take until it was actually played.

Two Timings for the Same Condition

There is a subtle design choice hiding inside every condition-controlled loop: does the condition get checked before the first run, or only after? Compare:

WHILE tickets remain > 0            REPEAT
    sell a ticket                       sell a ticket
ENDWHILE                             UNTIL tickets remain = 0

A WHILE loop checks first β€” if there are already 0 tickets when the loop begins, the body never runs at all, which is correct behaviour for a sold-out show. A REPEAT...UNTIL loop checks after β€” the body always runs at least once, even if the condition was already true at the very start. Neither style is universally "better"; the right choice depends on whether the situation can ever legitimately need zero repeats. Getting this choice wrong is a subtle but real source of off-by-one-style bugs, so when designing a loop, always ask: could this legitimately need to run zero times?

Key Words

  • Repetition / iteration / loop β€” running a group of steps more than once
  • Count-controlled loop β€” repeats a fixed number of times
  • Condition-controlled loop β€” repeats until a condition becomes true
  • Infinite loop β€” a loop whose exit condition is never met, so it never stops