← IGCSE Computer Science Paper 2

Iteration inside selection

A loop written inside an IF branch only runs when that branch is chosen. If the condition is FALSE, it is skipped. NEXT ends the loop first, then ENDIF ends the IF.

Understanding Iteration inside selection in IGCSE Computer Science Paper 2

What it does: The FOR loop is closed by NEXT N first, and then ENDIF closes the IF that contains it. When Total is not above 100, the FOR loop in THEN never starts, so only the ELSE OUTPUT runs.

Common mistakes: PRINT is not a Cambridge pseudocode keyword. Inside the loop, which only runs when Mark >= 50, the statement should be OUTPUT i. Cambridge pseudocode ends a FOR loop with NEXT and the counter, not ENDFOR. It should be NEXT i, and ENDIF then closes the IF.

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
IF Score > 50 THEN FOR i <- 1 TO 3 OUTPUT "Pass" NEXT i ENDIF. Score is 40. What happens?
The FOR loop is skipped and nothing is output"Pass" is output 3 times
Matches
IF X > 0 THEN FOR I <- 1 TO 3 OUTPUT I ENDIF NEXT I
↓
ENDIF is placed before NEXT I. The loop is inside the IF, so NEXT I must come first and ENDIF after it.
Applying
Meanings
Term
IF...THEN FOR...NEXT...ENDIF
Meaning
A loop written inside an IF branch only runs when that branch is chosen. If the condition is FALSE, it is skipped. NEXT ends the loop first, then ENDIF ends the IF.
Practice
IF Score >= 50 THEN
Testing
Gaps
Count <- 1 TO 3
Check Answer
Spots
2 FOR i <- 1 TO 3
3 PRINT i

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

The 'Cosmic Owl' badge — Comprehension Guru, 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. IF Score > 50 THEN FOR i <- 1 TO 3 OUTPUT "Pass" NEXT i ENDIF. Score is 40. What happens?
A. The FOR loop is skipped and nothing is output ✓
B. "Pass" is output 3 times
The condition Score > 50 is FALSE, so the THEN branch is not chosen. The loop inside it never runs.
2. Which is the correct order to close a FOR loop inside an IF?
A. ENDIF, then NEXT i
B. NEXT i, then ENDIF ✓
The loop is inside the IF, so it must finish first. NEXT ends the loop, then ENDIF ends the IF.
3. IF Age >= 18 THEN FOR i <- 1 TO 2 OUTPUT i NEXT i ENDIF. Age is 20. What is output?
A. Nothing is output
B. 1 then 2 ✓
Age >= 18 is TRUE, so the THEN branch is chosen and the FOR loop runs twice, outputting 1 then 2.
4. A WHILE loop is inside the THEN branch of an IF. Where does ENDIF go?
A. After ENDWHILE ✓
B. Before ENDWHILE
The WHILE loop must be fully closed with ENDWHILE before the IF is closed with ENDIF.
5. A FOR loop is written inside the ELSE branch. When does it run?
A. Only when the IF condition is FALSE ✓
B. Every time the IF statement is reached
The ELSE branch is chosen only when the condition is FALSE, so the loop inside it runs only then.

Match the Pairs

IF Age >= 18 THEN FOR I <- 1 TO 3 OUTPUT I NEXT I ENDIF → The FOR loop runs three times only when Age is 18 or more. Otherwise the whole loop is skipped.
IF Score > 50 THEN OUTPUT "Pass" ELSE WHILE Score < 50 DO Score <- Score + 10 ENDWHILE ENDIF → The WHILE loop is inside the ELSE branch, so it only runs when Score > 50 is FALSE.
The ENDIF comes after NEXT, because the loop is closed first and then the IF is closed → IF Go = TRUE THEN FOR N <- 1 TO 5 OUTPUT N NEXT N ENDIF
If the condition is FALSE, the loop in the THEN branch is skipped and the ELSE part runs instead → IF Total > 100 THEN FOR I <- 1 TO 3 OUTPUT "Big" NEXT I ELSE OUTPUT "Small" ENDIF
IF X > 0 THEN FOR I <- 1 TO 3 OUTPUT I ENDIF NEXT I → ENDIF is placed before NEXT I. The loop is inside the IF, so NEXT I must come first and ENDIF after it.
IF Ready = TRUE THEN WHILE Count < 5 DO Count <- Count + 1 ENDIF ENDWHILE → The WHILE loop is closed with ENDIF and the IF is closed with ENDWHILE, so the end keywords are the wrong way round.

Key Term

IF...THEN FOR...NEXT...ENDIF
A loop written inside an IF branch only runs when that branch is chosen. If the condition is FALSE, it is skipped. NEXT ends the loop first, then ENDIF ends the IF.

Fill in the Blanks

1. IF Age >= 18 THEN FOR Count <- 1 TO 3 OUTPUT "Welcome"
Hint: Type the keyword that starts a count-controlled loop inside the THEN branch.
2. OUTPUT Count NEXT Count ENDIF
Hint: Type the keyword that closes the selection after the loop has already ended.
3. ELSE WHILE Tries < 3 DO Tries <- Tries + 1
Hint: Type the keyword that starts a pre-condition loop inside the ELSE branch.
4. N <- N - 1 ENDWHILE ENDIF
Hint: Type the keyword that closes the pre-condition loop before the IF is closed.
5. CASE OF Choice 1 : FOR Count <- 1 TO 5 OUTPUT Count
Hint: Type the keyword that gives the end value of the loop that runs only when this CASE clause is chosen.

Spot the Error

Question 1
A IF Mark >= 50 THEN
B FOR i <- 1 TO 3
C PRINT i
D NEXT i
E ENDIF
Line C — PRINT is not a Cambridge pseudocode keyword. Inside the loop, which only runs when Mark >= 50, the statement should be OUTPUT i.
Question 2
A IF Lives > 0 THEN
B FOR i <- 1 TO Lives
C OUTPUT i
D ENDFOR
E ENDIF
Line D — Cambridge pseudocode ends a FOR loop with NEXT and the counter, not ENDFOR. It should be NEXT i, and ENDIF then closes the IF.
Question 3
A IF Total > 100
B WHILE Total > 100 DO
C Total <- Total - 10
D ENDWHILE
E ENDIF
Line A — The IF line is missing THEN. It should be IF Total > 100 THEN, so the WHILE loop inside only runs when the condition is TRUE.
Question 4
A IF Answer = "Y" THEN
B FOR Count = 1 TO 5
C OUTPUT "Hello"
D NEXT Count
E ENDIF
Line B — A FOR loop sets its counter with the assignment arrow <-, not =. It should be FOR Count <- 1 TO 5.
Question 5
A CASE OF Choice
B 1 : FOR i <- 1 TO 3
C OUTPUT i
D ENDIF
E ENDCASE
Line D — The FOR loop inside the CASE clause must be closed with NEXT i. ENDIF does not belong here, because ENDCASE closes the CASE.

Student with a class code? Join your class →