GCSE · Computer Science · AQA · Spec 8525

Nested iteration

A clock's minute hand does a full lap before the hour hand moves on by one. A loop inside a loop works exactly the same way.

Computer Science · Algorithms

Trace table — dry run the code

Step through one line at a time. Keep your eyes on the two counter columns: i is the slow one, j is the fast one.

1FOR i ← 1 TO 3
2 FOR j ← 1 TO 2
3 OUTPUT i, j
4 ENDFOR
5ENDFOR
i
j
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Counting what a nested loop does

Every row, every column

A program visits every cell of a grid with 3 rows and 4 columns. The outer loop picks the row (i); the inner loop picks the column (j). Walk down any branch to see which cell it reaches.

Outer loop: i picks the row → Inner loop: j picks the column

  • Every path starts here. Choose a row first — that's the outer loop's job.

3 × 4 = 12 cells visited, and the tree ends 12 times.

On Program starts. 3 branches to choose from.

Reading the ends from left to right gives the exact order the nested loop visits the cells. The same shape produces every combination of two sets of values — a multiplication table is just this, with i × j in each cell.

Watch out: The counts multiply, they don't add. Three rows of four columns is 3 × 4 = 12 visits, not 3 + 4 = 7. This works when both loops have a fixed number of rounds.

Mixing loop types

Which kind of loop is inside which?

For each task, decide what kind the OUTER loop is and what kind the INNER loop is, then put it in the matching group.

Still to sort

Count-controlled inside count-controlled (0)

Both loops know how many rounds they'll do before they start.

Count-controlled inside condition-controlled (0)

The outer loop repeats until something happens; each time, the inner loop does a fixed number of rounds.

Where the line is: Swap them round and you get the next group: there the fixed count is on the OUTSIDE.

Condition-controlled inside count-controlled (0)

A fixed number of outer rounds; inside each one, a loop that keeps going until a condition is met.

Condition-controlled inside condition-controlled (0)

Neither loop can know in advance how many rounds it will take.

6 of 6 still to sort.

A count-controlled loop repeats a set number of times. A condition-controlled loop repeats until (or while) something is true. You can put either kind inside either kind.

Watch out: Judge each loop by itself. A loop doesn't become condition-controlled just because it sits inside one.

Your turn to write one

Build a times-table program

Complete the program so it prints i × j for every i from 1 to 3 and every j from 1 to 4 — one row of four products for each value of i — and prints "End of row" after each row.

  1. FOR i ← 1 TO 3The outer loop: one pass for each row.
  2. missing step
Which line is step 2?

Spot the bug

Why does row 1 print, and nothing else?

This program should print a label for every seat in a small room: rows 1 to 3, with seats 1 to 4 in each row. When it runs, it prints the four seats of row 1 and then stops printing. Which line is wrong?

A student's program — which line goes wrong?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Put one loop inside another, and the inner one runs all the way through every time the outer one goes round once.

What you need to know

  • Say what nested iteration is: one loop inside the body of another.
  • Trace a nested loop, keeping track of both counters and the order of the output.
  • Work out how many times the inner body runs when both loops have fixed counts.
  • Recognise count-controlled and condition-controlled loops nested in any combination.
  • Write and complete nested loops, using indentation and ENDFOR / ENDWHILE to show which lines belong where.

The big picture

Nested iteration means putting one loop completely inside another. Each time the outer loop goes round once, the inner loop runs through all of its iterations from the start, with its counter reset. When both loops have fixed counts, the inner body runs outer count × inner count times.

Key points

1Nested iteration: one loop placed completely inside the body of another. The outer loop contains the inner loop.
2For every single pass of the outer loop, the inner loop runs all of its passes. Only when it finishes does control go back to the outer loop.
3Each loop has its own counter (say i outside, j inside). The inner counter resets every time the inner loop starts; the outer counter changes once per outer pass.
4When both loops have fixed counts, the inner body runs outer × inner times: multiply the two counts, don't add them.
5Any kind of loop can be nested in any kind: count-controlled (FOR) or condition-controlled (WHILE, REPEAT … UNTIL).
6Indentation and matching terminators (ENDFOR, ENDWHILE) show which statements belong to the inner loop and which to the outer loop.

Worked example

Problem

An outer loop FOR i ← 1 TO 3 contains an inner loop FOR j ← 1 TO i, and the inner loop's body is OUTPUT j. What does the program output?

⚠ Watch out

Adding the loop counts instead of multiplying them. An outer loop that runs 2 times around an inner loop that runs 5 times makes the inner body run 10 times, not 7 — the inner loop does all 5 of its rounds on each of the 2 outer passes.

🧠

Memory hook

Minute hand, hour hand: the inner loop does a full lap, every time, before the outer loop moves on one.

✓

Check yourself

FOR a ← 1 TO 5 holds FOR b ← 1 TO 2. How many inner runs are there, and what are a and b on run 4? (10; a = 2, b = 2)

Flashcards

(14)
What is nested iteration?
One loop placed completely inside the body of another loop.
The outer loop goes round once. What does the inner loop do?
It runs through all of its iterations, starting from the beginning.
When does control go back to the outer loop?
Only once the inner loop has finished all of its iterations.
Why does each loop in a nest need its own counter?
So the inner loop can count its rounds without disturbing the outer loop's count — for example i outside, j inside.
What happens to the inner loop's counter each time the inner loop starts again?
It is reset to its starting value.
How often does the outer loop's counter change?
Once per outer iteration. It holds still while the inner loop runs.
Both loops have fixed counts. How many times does the inner body run?
Outer count × inner count.
Outer loop: 6 rounds. Inner loop: 3 rounds. How many times does the inner body run?
18 (6 × 3) — not 9.
When does outer × inner NOT give the number of inner runs?
When the inner loop does a different number of rounds on different outer passes. Then count each pass and add them up.
Can a count-controlled loop go inside a condition-controlled loop?
Yes. Any kind of loop can be nested inside any kind, including two of the same kind.
Name two jobs nested iteration is good for.
Processing every row and every column of a grid, and producing every combination of two sets of values (such as a multiplication table).
How can you tell which lines are inside the inner loop?
By their indentation, and by which ENDFOR or ENDWHILE closes the block they sit in.
Where does a statement go if it must run once per outer pass?
Inside the outer loop but outside the inner one: after the inner loop's terminator, before the outer loop's.
Why must a WHILE inner loop's counter be set inside the outer loop?
A WHILE loop doesn't reset anything by itself. Set the counter outside and the inner loop won't run again on later passes.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 30 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.