← IGCSE Computer Science Paper 2

Error: wrong loop type

Using REPEAT when the body must sometimes not run, or WHILE when it must run at least once. REPEAT always runs once; WHILE may run zero times.

Understanding Error: wrong loop type in IGCSE Computer Science Paper 2

What it does: REPEAT ... UNTIL always runs the body once, so Password is read before it is tested. WHILE tests before the body, so if the first Mark is -1 the body is skipped and nothing is added to Total.

Common mistakes: Stock might already be 0, so the body must be able to run zero times. REPEAT always runs once and would sell an item that does not exist. Use WHILE Stock > 0 DO (and end with ENDWHILE). Pin has no value before the first INPUT, and WHILE tests before the body runs, so the INPUT may never happen. The body must run at least once, so use REPEAT (ending with UNTIL Pin = 1234).

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
A loop must be able to run zero times if there is no data. Which loop type is correct?
REPEAT ... UNTILWHILE ... DO ... ENDWHILE
Matches
REPEAT INPUT Score Count ← Count + 1 UNTIL Score = 0
↓
Wrong loop type: REPEAT runs with no data. If the first Score is 0 the sentinel is still counted. Use INPUT first, then WHILE Score <> 0 DO.
Applying
Meanings
Term
wrong loop type
Meaning
Using REPEAT when the body must sometimes not run, or WHILE when it must run at least once. REPEAT always runs once; WHILE may run zero times.
Practice
Total ← 0
Testing
Gaps
Count < NumItems DO
Check Answer
Spots
1REPEAT
2 Stock ← Stock - 1

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

The 'Gila Monster' badge — Patient Coder, 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 loop must be able to run zero times if there is no data. Which loop type is correct?
A. WHILE ... DO ... ENDWHILE ✓
B. REPEAT ... UNTIL
WHILE tests its condition before the body, so it may run zero times. REPEAT always runs its body once before testing.
2. A program reads scores into Total using REPEAT ... UNTIL Score = -1. The user types -1 straight away because there is no data. What goes wrong?
A. The -1 is added to Total, because REPEAT runs its body once before testing ✓
B. The loop is skipped and the body never runs
REPEAT always runs once, so the body processes the -1 as if it were a real score. A WHILE loop would skip the body.
3. A menu must be shown at least once before the user makes a choice. Which loop type is correct?
A. WHILE ... DO ... ENDWHILE
B. REPEAT ... UNTIL ✓
The menu body must run at least once, and REPEAT guarantees that. WHILE might not run at all if its condition is false at the start.
4. WHILE Answer <> "Y" DO is written, but Answer has not been given a value by INPUT yet. What is the error?
A. WHILE tests Answer before the first INPUT, so the condition is checked on a missing value ✓
B. WHILE always runs once, so the test is not needed
WHILE checks its condition first. The value must be input before the loop starts, or the loop type must be changed to REPEAT.
5. Which is a correct fix for a WHILE loop that tests Number before any INPUT?
A. Put INPUT Number after ENDWHILE
B. Put INPUT Number before the loop and again at the end of the loop body ✓
The first value must exist before WHILE tests it, and each pass needs a new INPUT. An INPUT after ENDWHILE is too late to help the loop. Changing to REPEAT is the other valid fix.

Match the Pairs

REPEAT INPUT Mark Total ← Total + Mark UNTIL Mark = -1 → REPEAT always runs once, so if -1 is the first value typed, it is still added to Total. A WHILE loop is needed here.
WHILE Answer <> "yes" DO OUTPUT "Go again?" INPUT Answer ENDWHILE → WHILE tests first, so if Answer has no value yet the check is wrong and the loop may never run. REPEAT ... UNTIL is needed.
Correct loop type when the body must run at least once: read the password first, then test it → REPEAT INPUT Password UNTIL Password = "abc"
Correct loop type when the body might not run at all: test the first INPUT before doing any work → INPUT Mark WHILE Mark <> -1 DO Total ← Total + Mark INPUT Mark ENDWHILE
REPEAT INPUT Score Count ← Count + 1 UNTIL Score = 0 → Wrong loop type: REPEAT runs with no data. If the first Score is 0 the sentinel is still counted. Use INPUT first, then WHILE Score <> 0 DO.
WHILE Guess <> Secret DO INPUT Guess ENDWHILE → Wrong loop type: WHILE is tested before the first INPUT, so Guess has no value yet. Change to REPEAT INPUT Guess UNTIL Guess = Secret.

Key Term

wrong loop type
Using REPEAT when the body must sometimes not run, or WHILE when it must run at least once. REPEAT always runs once; WHILE may run zero times.

Fill in the Blanks

1. Count ← 0 WHILE Count < NumItems DO Count ← Count + 1
Hint: Type the loop keyword that tests the condition first, so the body can be skipped when NumItems is 0
2. INPUT Age WHILE Age < 0 OR Age > 120 DO OUTPUT "Invalid age"
Hint: Type the keyword that ends the WHILE condition and begins the loop body
3. REPEAT INPUT Password UNTIL Password = "Secret"
Hint: Type the keyword that starts a loop whose body must always run at least once
4. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Hint: Type the logical operator that makes both parts of the range check true before the loop stops
5. INPUT Number WHILE Number <> 0 DO Total ← Total + Number
Hint: Type the relational operator meaning not equal, so the loop skips the body if the first value is the stop value

Spot the Error

Question 1
A REPEAT
B Stock ← Stock - 1
C OUTPUT "Sold one"
D ENDWHILE
Line A — Stock might already be 0, so the body must be able to run zero times. REPEAT always runs once and would sell an item that does not exist. Use WHILE Stock > 0 DO (and end with ENDWHILE).
Question 2
A DECLARE Pin : INTEGER
B OUTPUT "Enter your PIN"
C WHILE Pin <> 1234 DO
D INPUT Pin
E UNTIL Pin = 1234
Line C — Pin has no value before the first INPUT, and WHILE tests before the body runs, so the INPUT may never happen. The body must run at least once, so use REPEAT (ending with UNTIL Pin = 1234).
Question 3
A Total ← 0
B INPUT Mark
C REPEAT
D Total ← Total + Mark
E INPUT Mark
F ENDWHILE
Line C — The user may type -1 straight away, meaning there is no data. REPEAT would still run once and add -1 to Total. WHILE Mark <> -1 DO tests first, so the body is skipped when there is no data.
Question 4
A INPUT Target
B Distance ← 0
C OUTPUT "Start walking"
D REPEAT
E Distance ← Distance + 2
F ENDWHILE
Line D — If Target is 0 the walker should not move at all. REPEAT always runs the body once, so Distance would increase when it should not. Use WHILE Distance < Target DO so the body can run zero times.
Question 5
A INPUT Seats
B REPEAT
C OUTPUT "Seat sold"
D Seats ← Seats - 1
E ENDWHILE
Line B — If there are 0 seats left, nothing should be sold. REPEAT runs the body once before testing, so it would sell a seat that does not exist. Use WHILE Seats > 0 DO.

Student with a class code? Join your class →