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.
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.
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.
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.
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.
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
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?
FOR inside FOR: 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?
In a trace table of a nested loop, which counter column changes fastest?
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?
In a trace table, what happens to the inner counter when the inner loop starts again?
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
- Less than (relational operator)
- Less than or equal to (relational operator)
- Local variables
- Logic errors
- Meaningful identifier names
- Multiplication (arithmetic operator)
- Nested selection
- NOT (Boolean operator in code)
- Not equal to (relational operator)
- One-dimensional arrays
- OR (Boolean operator in code)
- Output to display
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.