← IGCSE Computer Science Paper 2

Three levels of nesting

Putting one structure inside another, three levels deep. Indent each level and close them in reverse order: innermost first, e.g. ENDIF, ENDIF, then NEXT.

Understanding Three levels of nesting in IGCSE Computer Science Paper 2

What it does: The IF is innermost so ENDIF comes first. The FOR is in the middle so NEXT comes next. The WHILE is outermost so ENDWHILE comes last. The FOR is at level 1, the first IF is indented to level 2, the second IF to level 3, and the OUTPUT sits inside all three. Each nested structure is indented one more level, so you can see which structure owns which lines. Closing statements are written in reverse order of opening: IF opened last, so it closes first. Understand the innermost structure first, then see how the structures around it control when it runs.

Common mistakes: The outer IF is missing THEN. Every IF condition must end with THEN, so the line should be IF Rain > 10 THEN. A WHILE loop uses DO, not THEN. THEN belongs to IF. The line should be WHILE Total < 100 DO.

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 contains an IF, and that IF contains another IF. Which closing order is correct?
ENDIF, ENDIF, then NEXTNEXT, ENDIF, then ENDIF
Matches
FOR i <- 1 TO 5 IF a > 0 THEN IF b > 0 THEN OUTPUT i ENDIF NEXT i ENDIF
↓
NEXT i closes the FOR before the outer IF has been closed. The structures overlap instead of nesting. The outer ENDIF must come before NEXT i.
Applying
Meanings
Term
nesting
Meaning
Putting one structure inside another, three levels deep. Indent each level and close them in reverse order: innermost first, e.g. ENDIF, ENDIF, then NEXT.
Practice
FOR Number ← 1 TO 20
Testing
Gaps
IF Rain = 0
Check Answer
Spots
1FOR Day <- 1 TO 7
2 IF Rain > 10

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

The 'Chambered Nautilus' badge — File Explorer, 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 contains an IF, and that IF contains another IF. Which closing order is correct?
A. ENDIF, ENDIF, then NEXT ✓
B. NEXT, ENDIF, then ENDIF
Close the innermost structure first. The inner IF ends first, then the outer IF, then the FOR loop ends with NEXT.
2. Which statement about indentation in a three-level nested algorithm is true?
A. All three levels are written at the same indentation
B. Each level is indented one step more than the structure around it ✓
Each nested structure is indented one more level, so you can see which structure owns which lines.
3. A WHILE loop contains a FOR loop, and the FOR loop contains an IF. Which structure is closed first?
A. ENDIF ✓
B. ENDWHILE
The IF is the innermost structure, so ENDIF comes first. Then NEXT closes the FOR, and ENDWHILE closes the WHILE last.
4. Which shows the correct closing order for WHILE containing FOR containing IF?
A. ENDWHILE, NEXT, ENDIF
B. ENDIF, NEXT, ENDWHILE ✓
Closing statements are written in reverse order of opening: IF opened last, so it closes first.
5. How should you read a three-level nested algorithm to work out what it does?
A. From the inside out, starting with the innermost structure ✓
B. Only the outermost structure matters, so ignore the rest
Understand the innermost structure first, then see how the structures around it control when it runs.

Match the Pairs

ENDIF ENDIF NEXT i → Closes the inner IF first, then the outer IF, then the FOR loop. This is the reverse of the order they were opened.
WHILE n > 0 DO FOR i <- 1 TO 3 IF i = n THEN → Opens three levels of nesting: a WHILE containing a FOR containing an IF, each indented one level further than the one before.
A WHILE containing a FOR containing an IF must be closed in reverse order: ENDIF, then NEXT, then ENDWHILE. → ENDIF NEXT j ENDWHILE
Indent each level of nesting one step further, so the innermost statement sits furthest to the right. → FOR i <- 1 TO 3 IF i > 1 THEN IF i < 3 THEN OUTPUT i
FOR i <- 1 TO 5 IF a > 0 THEN IF b > 0 THEN OUTPUT i ENDIF NEXT i ENDIF → NEXT i closes the FOR before the outer IF has been closed. The structures overlap instead of nesting. The outer ENDIF must come before NEXT i.
WHILE n > 0 DO FOR i <- 1 TO 3 IF i = n THEN OUTPUT i ENDIF NEXT i → The outermost WHILE loop is never closed. ENDWHILE is missing after NEXT i.

Key Term

nesting
Putting one structure inside another, three levels deep. Indent each level and close them in reverse order: innermost first, e.g. ENDIF, ENDIF, then NEXT.

Fill in the Blanks

1. FOR Day <- 1 TO 7 IF Temp > 20 THEN IF Rain = 0 THEN
Hint: Type the keyword that ends the condition line of the innermost IF.
2. ENDIF NEXT Count ENDWHILE
Hint: Type the keyword that closes the outermost loop, after the inner structures are closed.
3. WHILE Total < 100 DO FOR Count <- 1 TO 5 IF Mark > 50 THEN
Hint: Type the keyword that finishes the WHILE line before its body.
4. WHILE Total < 100 DO FOR Count <- 1 TO 5 IF Mark > 50 THEN
Hint: Type the keyword that starts the count-controlled loop in the middle level.
5. ENDIF ENDIF NEXT Day
Hint: Type the keyword that closes the outer count-controlled loop, after both ENDIFs.

Spot the Error

Question 1
A FOR Day <- 1 TO 7
B IF Rain > 10
C IF Temp < 15 THEN
D ENDIF
E ENDIF
F NEXT Day
Line B — The outer IF is missing THEN. Every IF condition must end with THEN, so the line should be IF Rain > 10 THEN.
Question 2
A WHILE Total < 100 THEN
B FOR N <- 1 TO 5
C IF N MOD 2 = 0 THEN
D ENDIF
E NEXT N
F ENDWHILE
Line A — A WHILE loop uses DO, not THEN. THEN belongs to IF. The line should be WHILE Total < 100 DO.
Question 3
A FOR S <- 1 TO 30
B IF Mark[S] >= 50 THEN
C IF Mark[S] >= 80 THEN
D ENDWHILE
E ENDIF
F NEXT S
Line D — The innermost structure is an IF, so it must be closed first with ENDIF. ENDWHILE belongs to a WHILE loop, and there is none here.
Question 4
A WHILE Lives > 0 DO
B FOR Shot <- 1 UNTIL 3
C IF Hit = TRUE THEN
D ENDIF
E NEXT Shot
F ENDWHILE
Line B — A FOR loop gives its end value with TO, not UNTIL. UNTIL belongs to REPEAT. The line should be FOR Shot <- 1 TO 3.
Question 5
A FOR Seat <- 1 TO 40
B IF Seat < 21 THEN
C IF Seat MOD 2 == 0 THEN
D ENDIF
E ENDIF
F NEXT Seat
Line C — Cambridge pseudocode tests equality with a single =, not ==. The line should be IF Seat MOD 2 = 0 THEN.

Student with a class code? Join your class →