GCSE · Computer Science · AQA · Spec 8525

Nested iteration

A clock's minute hand does a full lap before the hour hand moves on one. Put a FOR loop inside another and you get exactly that.

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 lands on. Before you start: how many cells do you reckon there are to visit?

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

  • Every path starts here. Pick 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.

Read the ends from top to bottom and you get the exact order the nested loop visits the cells. Same shape, any 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. That works because both loops have a fixed number of rounds.

Mixing loop types

Which kind of loop is inside which?

For each task, work out what kind the OUTER loop is and what kind the INNER loop is, then drop 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: Flip them round and you're in 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. Either kind can go inside either kind.

Watch out: Judge each loop on its own. A loop doesn't turn condition-controlled just because it sits inside one.

Your turn to write one

Build a times-table program

Your turn to build one. 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?

Find the bug

Why does "End of row" print after every seat?

This program should print a label for every seat in a small room (rows 1 to 3, seats 1 to 4 in each row) and print "End of row" once at the end of each row. Instead, "End of row" appears after every single seat. Which line is to blame?

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 right through every time the outer one goes round once.

What you need to know

  • Nested iteration means one loop sits completely inside the body of another: the outer loop contains the inner loop.
  • Every time the outer loop goes round once, the inner loop runs until it finishes. Only then does control return.
  • Give each loop its own counter, say i outside and j inside, so neither loop messes up the other's count.
  • The inner loop's counter is reset every time the inner loop starts again, while the outer counter changes just once per outer pass.
  • Have a goFOR j ← 4 TO 6 sits inside an outer loop. j has just finished at 6 and the outer loop moves on. What is j's first value on the next pass?

    4

    A FOR loop sets its counter from its own start value every time it starts, so j goes back to 4: not on to 7, and not back to 1.

  • In a trace table, i holds still while j runs through its values, then j starts again when i moves on.
  • With fixed counts, the inner body runs outer count × inner count times. Multiply, never add.
  • Have a goA nested loop with fixed counts runs its inner body 12 times. Sam doubles BOTH counts and confidently announces it will now run 24 times. Is Sam right?

    No. It runs 48 times, four times as many.

    The two counts multiply, so doubling each one doubles the answer twice: 2 × 2 = 4 times as many. Sam's 24 is what you'd get by doubling only one count.

  • So 6 outer passes of 3 inner rounds each means the inner body runs 6 × 3 = 18 times.
  • Any loop kind can nest inside any kind: count-controlled (FOR) or condition-controlled (WHILE, REPEAT … UNTIL).
  • Nesting is the tool for every row and column of a grid, or every combination of two sets, like a times table.
  • Have a goYou want to print a label for every seat in a cinema with 10 rows of 15 seats, finishing one row before starting the next. Which loop goes on the outside?

    The row loop is outer; the seat loop is inner.

    The inner loop's counter changes fastest, so seats 1 to 15 all run within each row before the row moves on. Swap them and you'd label seat 1 of every row first.

  • Indentation and matching ENDFOR / ENDWHILE show which lines belong to the inner loop and which to the outer.

The big picture

Nested iteration means putting one loop completely inside the body of another. Each time the outer loop goes round once, the inner loop runs through its iterations, and control only goes back to the outer loop when it has finished. The inner loop's counter is reset every time the inner loop starts again. 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 its iterations. Only when it finishes does control go back to the outer loop.
3Each loop has its own counter (say i outside, j inside). The outer counter changes once per outer pass.
4The inner loop's counter is reset every time the inner loop starts again, so each outer pass gives it a fresh run from the start.
5When both loops have fixed counts, the inner body runs outer × inner times: multiply the two counts, don't add them.
6Any kind of loop can be nested in any kind: count-controlled (FOR) or condition-controlled (WHILE, REPEAT … UNTIL).
7Indentation 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 2 contains an inner loop FOR j ← 1 TO 3, and the inner loop's body is OUTPUT i * j. What does the program output?

⚠ Watch out

Adding the loop counts instead of multiplying. An outer loop of 2 passes around an inner loop of 5 runs the inner body 10 times, not 7: the inner loop does all 5 rounds on EACH outer pass.

🧠

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 contains the inner loop.
FOR inside FOR: the outer loop goes round once. What does the inner loop do?
It runs through all of its iterations, with its counter starting again from the start value.
When does control go back to the outer loop?
Only once the inner loop has finished.
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 and j inside.
What happens to the inner loop's counter each time the inner loop starts again?
It is reset every time the inner loop starts again, so each pass of the outer loop gives the inner loop a fresh run from the start.
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. Multiply, don't add.
Outer loop: 6 rounds. Inner loop: 3 rounds. How many times does the inner body run?
18 (6 × 3), not 9.
In a trace table of a nested loop, which counter column changes fastest?
The inner loop's counter. It runs through its values and starts again while the outer counter holds still.
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.
In a trace table, what happens to the inner counter when the inner loop starts again?
Its counter goes back to the start value, while the outer counter has just moved on by one.

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 26 October 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.