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.
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.
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.
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.
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.
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
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?
The outer loop goes round once. What does the inner loop do?
When does control go back to the outer loop?
Why does each loop in a nest need its own counter?
What happens to the inner loop's counter each time the inner loop starts again?
How often does the outer loop's counter change?
Both loops have fixed counts. How many times does the inner body run?
Outer loop: 6 rounds. Inner loop: 3 rounds. How many times does the inner body run?
When does outer × inner NOT give the number of inner runs?
Can a count-controlled loop go inside a condition-controlled loop?
Name two jobs nested iteration is good for.
How can you tell which lines are inside the inner loop?
Where does a statement go if it must run once per outer pass?
Why must a WHILE inner loop's counter be set inside the outer loop?
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 postedMore AQA GCSE Computer Science topics
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.