← IGCSE Computer Science Paper 2

Nested iteration: a loop inside a loop

A loop placed inside another loop. The inner loop completes all its repeats for every single repeat of the outer loop, so a 3 by 4 nest runs the inner body 12 times.

Understanding Nested iteration: a loop inside a loop in IGCSE Computer Science Paper 2

What it does: Day 1 runs Lap 1 to 3, then Day 2 runs Lap 1 to 3 again, giving 6 outputs. For each Player value the WHILE loop runs until its condition is false, then the FOR loop moves on to the next Player. The inner loop finishes all 4 repeats for each of the 3 repeats of the outer loop, so the totals are multiplied: 3 × 4 = 12. Day stays at 1 while Hour counts 1, 2, 3. Only then does Day become 2 and Hour starts again at 1. Loops must be properly nested, so the inner NEXT must come before the outer NEXT.

Common mistakes: The inner loop reuses the outer counter Row, so the two loops fight over the same variable. The inner loop needs its own counter name: FOR Col <- 1 TO 4. A FOR loop sets its counter with the assignment arrow <-, not =. The line should be FOR Num <- 1 TO 5.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 389–398) and word search (Modules 379–398).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A FOR loop with counter Row runs 3 times and contains a FOR loop with counter Col that runs 4 times. How many times does the inner loop body run in total?
7 times (3 + 4)12 times (3 × 4)
Matches
FOR A <- 1 TO 3 FOR A <- 1 TO 2 OUTPUT A NEXT A NEXT A
↓
The inner and outer loops use the same counter name A, so the inner loop changes the outer counter. Use different names such as A and B.
Applying
Meanings
Term
nested loop
Meaning
A loop placed inside another loop. The inner loop completes all its repeats for every single repeat of the outer loop, so a 3 by 4 nest runs the inner body 12 times.
Practice
FOR Row ← 1 TO 3
Testing
Gaps
Col ← 1 TO 4
Check Answer
Spots
1FOR Row <- 1 TO 3
2 FOR Row <- 1 TO 4

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Narwhal' badge — Unicorn Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A FOR loop with counter Row runs 3 times and contains a FOR loop with counter Col that runs 4 times. How many times does the inner loop body run in total?
A. 7 times (3 + 4)
B. 12 times (3 × 4) ✓
The inner loop finishes all 4 repeats for each of the 3 repeats of the outer loop, so the totals are multiplied: 3 × 4 = 12.
2. Which statement describes what happens in nested loops?
A. The outer loop completes all its repeats for every single repeat of the inner loop
B. The inner loop completes all its repeats for every single repeat of the outer loop ✓
The inner loop is the one that runs fully each time round. The outer loop only moves on once the inner loop has finished.
3. Which nested FOR loops are written correctly?
A. FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row, Col NEXT Col NEXT Row ✓
B. FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row, Col NEXT Row NEXT Col
The inner loop must be closed first. NEXT Col comes before NEXT Row because the inner loop sits completely inside the outer loop.
4. Which pair of counter names is a correct choice for a FOR loop nested inside another FOR loop?
A. Outer loop uses Row, inner loop uses Col ✓
B. Outer loop uses Count, inner loop also uses Count
Each loop needs its own counter variable. If both used Count, the inner loop would change the outer loop's counter and break it.
5. An outer FOR loop runs 5 times. Inside it is an inner FOR loop that runs 2 times. How many times does the outer loop run in total?
A. 5 times ✓
B. 10 times
The outer loop runs only 5 times. It is the inner loop body that runs 5 × 2 = 10 times in total.

Match the Pairs

FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row * Col NEXT Col NEXT Row → A FOR loop inside a FOR loop: the inner body runs 3 x 4 = 12 times in total
NEXT Col NEXT Row → Closes the inner loop first, then closes the outer loop
The inner loop restarts and completes all its repeats every time the outer loop repeats once → FOR Day <- 1 TO 2 FOR Lap <- 1 TO 3 OUTPUT Day, Lap NEXT Lap NEXT Day
A WHILE loop can be nested inside a FOR loop, and the WHILE loop finishes before the next FOR repeat → FOR Player <- 1 TO 5 WHILE Score < 10 DO Score <- Score + 1 ENDWHILE NEXT Player
FOR A <- 1 TO 3 FOR A <- 1 TO 2 OUTPUT A NEXT A NEXT A → The inner and outer loops use the same counter name A, so the inner loop changes the outer counter. Use different names such as A and B.
FOR X <- 1 TO 3 FOR Y <- 1 TO 2 OUTPUT X, Y NEXT X NEXT Y → The NEXT statements are in the wrong order. The inner loop must end first: NEXT Y then NEXT X.

Key Term

nested loop
A loop placed inside another loop. The inner loop completes all its repeats for every single repeat of the outer loop, so a 3 by 4 nest runs the inner body 12 times.

Fill in the Blanks

1. FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 OUTPUT Row, Col
Hint: Type the keyword that starts the inner count-controlled loop.
2. OUTPUT Row, Col NEXT Col NEXT Row
Hint: Type the counter of the inner loop, which must be closed before the outer loop.
3. FOR Day ← 1 TO 5 FOR Lap ← 1 TO 3 OUTPUT "Run" // runs 15 times
Hint: Type the inner loop's upper limit so that 5 outer repeats give 15 runs in total.
4. FOR Round ← 1 TO 3 WHILE Score < 10 DO Score ← Score + 1
Hint: Type the keyword for a pre-condition loop placed inside the FOR loop.
5. WHILE Again = "Y" DO REPEAT INPUT Mark
Hint: Type the keyword that starts a post-condition loop inside the WHILE loop.

Spot the Error

Question 1
A FOR Row <- 1 TO 3
B FOR Row <- 1 TO 4
C OUTPUT "*"
D NEXT Col
E NEXT Row
Line B — The inner loop reuses the outer counter Row, so the two loops fight over the same variable. The inner loop needs its own counter name: FOR Col <- 1 TO 4.
Question 2
A FOR Table <- 1 TO 3
B OUTPUT "Table ", Table
C FOR Num = 1 TO 5
D OUTPUT Num
E NEXT Num
F NEXT Table
Line C — A FOR loop sets its counter with the assignment arrow <-, not =. The line should be FOR Num <- 1 TO 5.
Question 3
A FOR Row <- 1 UNTIL 3
B FOR Col <- 1 TO 4
C OUTPUT Row * Col
D NEXT Col
E NEXT Row
Line A — A FOR loop uses the keyword TO to give the end value, not UNTIL (UNTIL belongs to REPEAT). It should be FOR Row <- 1 TO 3.
Question 4
A FOR Player <- 1 TO 4
B FOR Round <- 1 TO 3
C OUTPUT Player, Round
D NEXT Player
E NEXT Player
Line D — The inner loop must be closed first with NEXT Round. Closing Player here crosses the loops over. The line should be NEXT Round, then NEXT Player on the last line.
Question 5
A Count <- 0
B FOR Row <- 1 TO 3
C FOR Col <- 4 TO 1
D Count <- Count + 1
E NEXT Col
F NEXT Row
Line C — The start value is bigger than the end value, so the inner loop never runs and Count stays 0. It should be FOR Col <- 1 TO 4, giving 3 x 4 = 12 repeats.

Student with a class code? Join your class →