← IGCSE Computer Science Paper 2

Error: missing ENDIF, NEXT, ENDWHILE or UNTIL

Closing keywords that end IF, FOR, WHILE and REPEAT. Each one must match its opening statement. A missing or mismatched closing keyword is an error.

Understanding Error: missing ENDIF, NEXT, ENDWHILE or UNTIL in IGCSE Computer Science Paper 2

What it does: REPEAT is closed by UNTIL followed by the condition. ENDWHILE only belongs to WHILE loops. Each IF is closed by its own ENDIF. The inner ENDIF closes the inner IF and the outer ENDIF closes the outer IF. The identifier after NEXT must be the same as the loop variable in the FOR line. NEXT Counter is a different identifier, so it is an error. Each IF needs its own ENDIF. A nested IF with only one ENDIF has a missing closing keyword. ENDIF is required. Without it the algorithm is incorrect because the end of the IF statement is not shown.

Common mistakes: The loop counter is now Num, but the loop is closed with NEXT Count. NEXT must use the same identifier as the FOR line. The first line should be FOR Count ← 1 TO 5. A WHILE loop must be closed with ENDWHILE. ENDIF closes an IF statement, so it is the wrong closing keyword here.

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 232–241) and word search (Modules 232–253).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which IF statement is closed correctly? A: IF Score > 50 THEN OUTPUT "Pass" ENDIF B: IF Score > 50 THEN OUTPUT "Pass" ENDWHILE
B: it ends with ENDWHILEA: it ends with ENDIF
Matches
FOR Count ← 1 TO 10 OUTPUT Count NEXT Total
↓
NEXT uses a different identifier (Total) from the FOR loop counter (Count). It should be NEXT Count.
Applying
Meanings
Term
ENDIF NEXT ENDWHILE UNTIL
Meaning
Closing keywords that end IF, FOR, WHILE and REPEAT. Each one must match its opening statement. A missing or mismatched closing keyword is an error.
Practice
FOR Count ← 1 TO 5
Testing
Gaps
NEXT
Check Answer
Spots
1FOR Num ← 1 TO 5
2 OUTPUT Count

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

The 'Desert Jerboa' badge — High Jumper, 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. Which IF statement is closed correctly? A: IF Score > 50 THEN OUTPUT "Pass" ENDIF B: IF Score > 50 THEN OUTPUT "Pass" ENDWHILE
A. A: it ends with ENDIF ✓
B. B: it ends with ENDWHILE
Every IF must be closed with ENDIF. ENDWHILE closes a WHILE loop, so it is a mismatched closing keyword.
2. A FOR loop starts with FOR Count ← 1 TO 5. Which closing line is correct?
A. NEXT Counter
B. NEXT Count ✓
The identifier after NEXT must be the same as the loop variable in the FOR line. NEXT Counter is a different identifier, so it is an error.
3. A student writes a REPEAT loop and ends it with ENDWHILE. Is this correct?
A. No. A REPEAT loop must end with UNTIL and a condition ✓
B. Yes. ENDWHILE can close any loop
ENDWHILE only closes WHILE ... DO. REPEAT is closed by UNTIL followed by a condition, for example UNTIL Pass = TRUE.
4. Which statement about a nested IF is true? IF A > 0 THEN IF B > 0 THEN OUTPUT "Both"
A. It needs two ENDIFs, one for each IF ✓
B. One ENDIF at the end closes both IF statements
Each IF needs its own ENDIF. A nested IF with only one ENDIF has a missing closing keyword.
5. Which WHILE loop is closed correctly? A: WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE B: WHILE Lives > 0 DO Lives ← Lives - 1 UNTIL Lives = 0
A. B: it ends with UNTIL
B. A: it ends with ENDWHILE ✓
WHILE ... DO is closed with ENDWHILE. UNTIL belongs to REPEAT, so using it here is a mismatched closing keyword.

Match the Pairs

NEXT Counter → Closes a FOR loop whose counter variable is Counter
ENDWHILE → Closes a WHILE ... DO loop
A REPEAT loop must end with UNTIL and a condition, not with ENDWHILE → REPEAT INPUT Number UNTIL Number > 0
A nested IF needs its own ENDIF, so two IF statements need two ENDIF keywords → IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Distinction" ENDIF ENDIF
FOR Count ← 1 TO 10 OUTPUT Count NEXT Total → NEXT uses a different identifier (Total) from the FOR loop counter (Count). It should be NEXT Count.
REPEAT INPUT Answer ENDWHILE → ENDWHILE has been used to close a REPEAT loop. It should be UNTIL with a condition, such as UNTIL Answer = 5.

Key Term

ENDIF NEXT ENDWHILE UNTIL
Closing keywords that end IF, FOR, WHILE and REPEAT. Each one must match its opening statement. A missing or mismatched closing keyword is an error.

Fill in the Blanks

1. FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Hint: Type the loop variable that NEXT must name to match the FOR line
2. IF Score > 50 THEN OUTPUT "Pass" ENDIF
Hint: Type the closing keyword that ends an IF statement
3. WHILE Total < 100 DO Total ← Total + 5 ENDWHILE
Hint: Type the closing keyword that ends a WHILE loop
4. REPEAT INPUT Guess UNTIL Guess = 7
Hint: Type the keyword that closes a REPEAT loop and starts its condition. ENDWHILE is not used here
5. IF Age > 0 THEN IF Age > 9 THEN OUTPUT "Big" ENDIF ENDIF
Hint: Type the closing keyword that is missing for the outer IF of this nested IF

Spot the Error

Question 1
A FOR Num ← 1 TO 5
B OUTPUT Count
C NEXT Count
Line A — The loop counter is now Num, but the loop is closed with NEXT Count. NEXT must use the same identifier as the FOR line. The first line should be FOR Count ← 1 TO 5.
Question 2
A Count ← 0
B WHILE Count < 3 DO
C Count ← Count + 1
D ENDIF
Line D — A WHILE loop must be closed with ENDWHILE. ENDIF closes an IF statement, so it is the wrong closing keyword here.
Question 3
A REPEAT
B INPUT Pin
C ENDWHILE
Line C — A REPEAT loop is closed by UNTIL followed by a condition. ENDWHILE belongs to a WHILE loop. The line should be UNTIL Pin = 1234.
Question 4
A IF Mark >= 50 THEN
B WHILE Mark >= 80 DO
C OUTPUT "High"
D ENDIF
E ENDIF
Line B — This line opens a WHILE loop, but the block is closed with ENDIF. The opening statement and closing keyword do not match. The line should be IF Mark >= 80 THEN.
Question 5
A FOR Row ← 1 TO 3
B FOR Num ← 1 TO 4
C OUTPUT Row * Col
D NEXT Col
E NEXT Row
Line B — The inner loop uses Num, but it is closed by NEXT Col. The identifier after NEXT must match the FOR line. The line should be FOR Col ← 1 TO 4.

Student with a class code? Join your class →