← IGCSE Computer Science Paper 2

Error: loop that never ends

A loop that never ends because nothing in the body changes the tested variable, or the condition can never reach its exit value.

Understanding Error: loop that never ends in IGCSE Computer Science Paper 2

What it does: Count ← Count + 1 changes the tested variable each time round, so Count <= 5 eventually becomes FALSE. Num is tested in the condition, so it must be updated in the body. Output after the loop does not change Num. Adding positive numbers makes Total bigger, so Total < 0 never becomes TRUE. The condition needs to be one that can be reached, such as Total > 100. Count goes up from 1, so Count >= 1 stays TRUE forever. In the other loop, Count will eventually pass 10 and the loop will stop. The exit condition is impossible to satisfy, so the REPEAT loop never stops however many values are entered.

Common mistakes: Count goes down, so it moves away from the exit value and Count <= 5 stays TRUE forever. The update must be Count ← Count + 1. Num only increases, so Num > 0 is always TRUE and the loop never stops. The test points the wrong way. It should be WHILE Num < 20 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 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
Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE Why does this loop never end?
Nothing in the body changes Count, so Count <= 5 is always TRUEOUTPUT cannot be used inside a WHILE loop
Matches
Count ← 10 WHILE Count > 0 DO Count ← Count + 1 ENDWHILE
↓
Wrong direction: Count goes up away from 0, so Count > 0 is always TRUE. Fix by using Count ← Count - 1.
Applying
Meanings
Term
infinite loop
Meaning
A loop that never ends because nothing in the body changes the tested variable, or the condition can never reach its exit value.
Practice
DECLARE Count : INTEGER
Testing
Gaps
Count ← + 1
Check Answer
Spots
3 OUTPUT Count
4 Count ← Count - 1

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. Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE Why does this loop never end?
A. OUTPUT cannot be used inside a WHILE loop
B. Nothing in the body changes Count, so Count <= 5 is always TRUE ✓
The variable tested in the condition must change inside the body. Adding Count ← Count + 1 before ENDWHILE fixes it.
2. Which change stops this loop from running forever? Num ← 10 WHILE Num > 0 DO OUTPUT Num ENDWHILE
A. Add Num ← Num - 1 inside the loop ✓
B. Add OUTPUT Num after ENDWHILE
Num is tested in the condition, so it must be updated in the body. Output after the loop does not change Num.
3. Total ← 0 REPEAT INPUT Number Total ← Total + Number UNTIL Total < 0 The user only enters positive numbers. What is the problem?
A. The UNTIL condition can never become TRUE, so the loop never ends ✓
B. REPEAT loops must always run exactly once
Adding positive numbers makes Total bigger, so Total < 0 never becomes TRUE. The condition needs to be one that can be reached, such as Total > 100.
4. Which loop has the test in the wrong direction and so never ends?
A. Count ← 1 WHILE Count <= 10 DO Count ← Count + 1 ENDWHILE
B. Count ← 1 WHILE Count >= 1 DO Count ← Count + 1 ENDWHILE ✓
Count goes up from 1, so Count >= 1 stays TRUE forever. In the other loop, Count will eventually pass 10 and the loop will stop.
5. A WHILE loop asks for a password: INPUT Password WHILE Password <> "abc123" DO OUTPUT "Try again" ENDWHILE What must be added to the body so it can end?
A. INPUT Password ✓
B. OUTPUT Password
The user must be able to enter a new value each time. Without INPUT Password inside the loop, Password never changes.

Match the Pairs

WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Infinite loop: Count is never changed in the body, so Count <= 5 stays TRUE forever
REPEAT INPUT Num UNTIL Num = 0 AND Num = 1 → Infinite loop: the UNTIL condition can never be TRUE because Num cannot be 0 and 1 at once
Adding an update to the loop body makes the tested variable move towards the exit value, so the loop ends → WHILE Count <= 5 DO OUTPUT Count Count ← Count + 1 ENDWHILE
Putting INPUT inside the loop body lets the user change the tested variable so the exit value can be reached → WHILE Answer <> "Q" DO OUTPUT "Enter Q to quit" INPUT Answer ENDWHILE
Count ← 10 WHILE Count > 0 DO Count ← Count + 1 ENDWHILE → Wrong direction: Count goes up away from 0, so Count > 0 is always TRUE. Fix by using Count ← Count - 1.
REPEAT INPUT Score UNTIL Score > 100 AND Score < 0 → The UNTIL condition can never be TRUE because no number is both above 100 and below 0. Fix by using OR instead of AND.

Key Term

infinite loop
A loop that never ends because nothing in the body changes the tested variable, or the condition can never reach its exit value.

Fill in the Blanks

1. WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Hint: Type the variable tested in the condition. It must be updated in the body so the loop can end.
2. WHILE Num < 10 DO Num ← Num + 1 ENDWHILE
Hint: Type the relational operator that lets the test become FALSE as Num counts upwards.
3. REPEAT Num ← Num - 1 UNTIL Num <= 0
Hint: Type the relational operator so the UNTIL condition can become TRUE as Num counts downwards.
4. WHILE Answer <> "yes" DO INPUT Answer ENDWHILE
Hint: Type the keyword that lets the body change the tested variable on each pass.
5. REPEAT INPUT Guess UNTIL Guess = 7
Hint: Type the keyword that ends the loop once its condition becomes TRUE.

Spot the Error

Question 1
A Count ← 1
B WHILE Count <= 5 DO
C OUTPUT Count
D Count ← Count - 1
E ENDWHILE
Line D — Count goes down, so it moves away from the exit value and Count <= 5 stays TRUE forever. The update must be Count ← Count + 1.
Question 2
A Num ← 2
B WHILE Num > 0 DO
C OUTPUT Num
D Num ← Num + 2
E ENDWHILE
Line B — Num only increases, so Num > 0 is always TRUE and the loop never stops. The test points the wrong way. It should be WHILE Num < 20 DO.
Question 3
A WHILE TRUE DO
B OUTPUT "Driving"
C Fuel ← Fuel - 5
D ENDWHILE
Line A — The condition TRUE can never become FALSE, so the loop never ends whatever the body does. It should test the variable that changes: WHILE Fuel > 0 DO.
Question 4
A Count ← 0
B REPEAT
C Count ← 1
D OUTPUT Count
E UNTIL Count = 10
Line C — Count is reset to 1 on every pass, so it never reaches 10 and UNTIL Count = 10 is never TRUE. The body must add to Count: Count ← Count + 1.
Question 5
A Score ← 0
B REPEAT
C Score ← Score + 5
D UNTIL Score = 102
Line D — Score goes up in steps of 5 (..., 100, 105), so it never equals 102. The UNTIL condition never becomes TRUE. It should be UNTIL Score = 100.

Student with a class code? Join your class →