← IGCSE Computer Science Paper 2

Nested loops in pseudocode

A loop placed inside another loop. The inner loop runs completely for every pass of the outer loop, so total passes = outer passes x inner passes.

Understanding Nested loops in pseudocode in IGCSE Computer Science Paper 2

What it does: Each level of nesting is indented by four spaces, so the structure of the loops can be seen at a glance. The outer loop runs 5 times and the inner loop runs 2 times on each of them, so OUTPUT runs 5 x 2 = 10 times. A nested loop needs one NEXT for every FOR. Without NEXT Column the inner loop is never closed.

Common mistakes: Assignment in pseudocode uses ←, not =. The line should be Total ← Total + 1 so the value is stored in Total on every pass of the inner loop. Count is not a loop variable here and has not been declared. The inner loop variable is Column, so the line should be OUTPUT Row * Column.

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 166–175) and word search (Modules 166–187).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A FOR loop from 1 TO 3 contains an inner FOR loop from 1 TO 4. How many times does the inner loop body run in total?
127
Matches
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Row
↓
The inner loop has no NEXT Column. Each loop needs its own NEXT, so NEXT Column must be added before NEXT Row.
Applying
Meanings
Term
nested loop
Meaning
A loop placed inside another loop. The inner loop runs completely for every pass of the outer loop, so total passes = outer passes x inner passes.
Practice
FOR Row ← 1 TO 3
Testing
Gaps
Column ← 1 TO 4
Check Answer
Spots
2 FOR Column ← 1 TO 4
3 Total = Total + 1

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

The 'Tree Frog' badge — Leap of Logic, 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 from 1 TO 3 contains an inner FOR loop from 1 TO 4. How many times does the inner loop body run in total?
A. 7
B. 12 ✓
Total passes = outer x inner = 3 x 4 = 12. The inner loop runs completely for each pass of the outer loop. Adding gives 7, which is wrong.
2. Which statement describes a nested loop correctly?
A. The inner loop runs completely for every pass of the outer loop ✓
B. The inner loop runs once, then the outer loop runs completely
The outer loop controls the repeats. Each time it passes, the inner loop starts again and finishes all of its passes.
3. Which pseudocode correctly closes two nested FOR loops (Row outside, Column inside)?
A. NEXT Column NEXT Row ✓
B. NEXT Row NEXT Column
The inner loop must finish first, so NEXT Column comes before NEXT Row. Each FOR needs its own NEXT, and the loops cannot cross over.
4. How many spaces should the inner FOR loop be indented inside the outer FOR loop?
A. The same as the outer FOR
B. Four spaces more than the outer FOR ✓
Each level of nesting is indented by four spaces. This shows clearly which statements belong to which loop.
5. An outer loop FOR Row ← 1 TO 5 contains an inner loop FOR Column ← 1 TO 2. How many times does the outer loop run?
A. 5 times ✓
B. 10 times
The outer loop runs 5 times. The inner loop body runs 5 x 2 = 10 times in total, but the outer loop is not affected by the inner loop.

Match the Pairs

FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Column NEXT Row → A nested loop: the inner Column loop runs 4 times for each of the 3 Row passes, so OUTPUT runs 3 x 4 = 12 times
NEXT Column NEXT Row → Each loop closes with its own NEXT, the inner loop first and then the outer loop
The inner loop is indented four more spaces than the outer loop, and its body four more again → FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 OUTPUT Column NEXT Column NEXT Row
Total passes = outer passes x inner passes, so an outer loop of 5 and an inner loop of 2 give 10 passes → FOR Row ← 1 TO 5 FOR Column ← 1 TO 2 OUTPUT "*" NEXT Column NEXT Row
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Row → The inner loop has no NEXT Column. Each loop needs its own NEXT, so NEXT Column must be added before NEXT Row.
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Row NEXT Column → The NEXT statements are in the wrong order. The inner loop must be closed first, so NEXT Column comes before NEXT Row.

Key Term

nested loop
A loop placed inside another loop. The inner loop runs completely for every pass of the outer loop, so total passes = outer passes x inner passes.

Fill in the Blanks

1. FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column
Hint: Type the keyword that starts the second loop placed inside the first loop
2. OUTPUT Row * Column NEXT Column NEXT Row
Hint: Type the keyword that ends the inner loop before the outer loop ends
3. FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Column
Hint: Type the counter variable of the inner loop that this NEXT closes
4. OUTPUT Row * Column NEXT Column NEXT Row
Hint: Type the counter variable of the outer loop that this last NEXT closes
5. Rows ← 3 Cols ← 4 Total ← Rows * Cols
Hint: Type the arithmetic operator that works out total passes from the outer and inner passes

Spot the Error

Question 1
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 4
C Total = Total + 1
D NEXT Column
E NEXT Row
Line C — Assignment in pseudocode uses ←, not =. The line should be Total ← Total + 1 so the value is stored in Total on every pass of the inner loop.
Question 2
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 2
C OUTPUT Row * Count
D NEXT Column
E NEXT Row
Line C — Count is not a loop variable here and has not been declared. The inner loop variable is Column, so the line should be OUTPUT Row * Column.
Question 3
A FOR Row ← 1 UNTIL 5
B FOR Column ← 1 TO 3
C OUTPUT Row, Column
D NEXT Column
E NEXT Row
Line A — A FOR loop sets its range with TO, not UNTIL (UNTIL belongs to REPEAT). The line should be FOR Row ← 1 TO 5.
Question 4
A FOR Row ← 1 TO 2
B FOR Column = 1 TO 4
C OUTPUT "#"
D NEXT Column
E NEXT Row
Line B — A FOR loop sets its starting value with the assignment arrow ←, not =. The line should be FOR Column ← 1 TO 4.
Question 5
A FOR Row ← 1 TO 3
B FOR Row ← 1 TO 3
C OUTPUT Row + Column
D NEXT Column
E NEXT Row
Line B — The inner loop reuses the outer loop's variable Row, so the two loops would clash. The inner loop needs its own variable: FOR Column ← 1 TO 3.

Student with a class code? Join your class →