Iteration (Loops)

💻 Computer Science Years 7–11 Foundation

Repeating code with count-controlled FOR loops and condition-controlled WHILE and REPEAT UNTIL loops.

Open this lesson, with practice and a printable worksheet →

Computers are brilliant at one thing above all: doing the same job over and over without getting bored. Iteration — looping — is how you tell a program to repeat a block of code. Instead of writing "print star" fifty times, you write it once and say "do that fifty times". Loops turn long, repetitive tasks into a few tidy lines.

Why this matters: Nearly every real program loops — scrolling a feed, checking each item in a shopping basket, dealing a hand of cards. Loops are the third building block of programming, and knowing which type to reach for is a core GCSE skill.
loop body repeat
A loop runs its body again and again, then exits.

Count-controlled loops (FOR)

FOR i ← 1 TO 512345stops on its own
A FOR loop steps a counter through a fixed range and stops by itself.

When you know exactly how many times to repeat, use a FOR loop. A counter variable steps through a range of numbers automatically.

FOR i ← 1 TO 5
    OUTPUT i
NEXT i

This outputs 1, 2, 3, 4, 5. Picture a runner doing five laps of a track: the loop counts each lap for you and stops on its own. This is the loop to reach for when the number of repeats is fixed.

Condition-controlled loops (WHILE)

total < 10 ?Truetotal = total + 3loop backFalseexittest first → body may run 0 times
WHILE tests before the body, so it can run zero times.

Sometimes you do not know how many repeats you need — you keep going until something happens. A WHILE loop checks a condition before each pass and stops as soon as it becomes False.

total = 0
WHILE total < 10
    total = total + 3
ENDWHILE

Like eating chips until you are full: you cannot say in advance how many, you just keep checking.

REPEAT UNTIL: check at the end

run the bodycorrect ?False:try againTrueexittest after → body runs at least once
REPEAT UNTIL tests after the body, so it always runs at least once.

A REPEAT ... UNTIL loop tests its condition after the body, so the body always runs at least once. Handy for asking a user to try again.

LoopUse whenTest happens
FORRepeats are counted / fixed—
WHILERepeats unknown, may be zeroBefore the body
REPEAT UNTILRepeats unknown, at least oneAfter the body
Worked trace of the WHILE loop above:
Start: total = 0. Is 0 < 10? Yes → total = 3.
Is 3 < 10? Yes → total = 6.
Is 6 < 10? Yes → total = 9.
Is 9 < 10? Yes → total = 12.
Is 12 < 10? No → stop. Final total = 12 (the loop ran 4 times).
Common mistake: an infinite loop. If a WHILE condition never becomes False — for example you forget to change total inside the loop — the program repeats forever. Always make sure something inside the loop moves you towards the exit.

Ask yourself one question to pick the loop: "Do I know the number of repeats?" Yes → FOR. No → WHILE (or REPEAT UNTIL if it must run at least once).

know how manyrepeats?YesFORNomust run atleast once?YesREPEATNoWHILE
One or two questions pick the right loop every time.
Get the printable worksheet →
← Selection (IF statements) → Arrays & Lists
← Sasta Basta home All subjects