← IGCSE Computer Science Paper 2

Re-asking for input until it is valid

A REPEAT loop around INPUT that keeps asking for a value and shows an error message each time the check fails. The UNTIL condition is the valid range, so the loop ends only on valid data.

Understanding Re-asking for input until it is valid in IGCSE Computer Science Paper 2

What it does: The WHILE loop tests first, so INPUT must come before it. The condition is the invalid range, the opposite of the REPEAT UNTIL condition. The error message is only useful while the data is wrong. Put an IF inside the loop to OUTPUT the message when the check fails. After the loop the value is already valid.

Common mistakes: A number cannot be below 1 and above 10 at the same time, so the error message would never show. Invalid data is below the range OR above it: IF Num < 1 OR Num > 10 THEN. Inside the WHILE loop the program must ask again with INPUT Pts. Outputting the old value never changes Pts, so the loop would never end.

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 60–70) and word search (Modules 47–70).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A student wants a program to keep asking for a mark until it is between 0 and 100. Which loop condition is correct?
REPEAT ... UNTIL Mark >= 0 AND Mark <= 100REPEAT ... UNTIL Mark < 0 OR Mark > 100
Matches
REPEAT INPUT Age UNTIL Age < 0 OR Age > 120
↓
The UNTIL condition describes invalid data, so the loop stops on an invalid age and keeps asking for valid ones. It should be UNTIL Age >= 0 AND Age <= 120.
Applying
Meanings
Term
REPEAT INPUT UNTIL valid
Meaning
A REPEAT loop around INPUT that keeps asking for a value and shows an error message each time the check fails. The UNTIL condition is the valid range, so the loop ends only on valid data.
Practice
REPEAT
Testing
Gaps
Age
Check Answer
Spots
2 INPUT Num
3 IF Num < 1 AND Num > 10 THEN

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

The 'Dumbo Octopus' badge — Module Maker, 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 student wants a program to keep asking for a mark until it is between 0 and 100. Which loop condition is correct?
A. REPEAT ... UNTIL Mark >= 0 AND Mark <= 100 ✓
B. REPEAT ... UNTIL Mark < 0 OR Mark > 100
With REPEAT ... UNTIL, the condition describes VALID data. The loop stops only when the mark is in the range 0 to 100. The wrong answer describes invalid data, so the loop would stop on bad input.
2. Where should the INPUT command go in a validation loop built with REPEAT ... UNTIL?
A. Inside the loop, so a new value is read every time ✓
B. Before the loop only, so it is read once
If INPUT is only before the loop, the value never changes and the loop cannot end (or never re-asks). The INPUT must be inside the loop so the user can type a new value.
3. Which statement about REPEAT ... UNTIL is true?
A. The condition is checked first, so the body might never run
B. The body always runs at least once, so the user is always asked once ✓
REPEAT ... UNTIL checks the condition at the END of the loop. That means the prompt and INPUT always happen at least once. A WHILE loop checks first.
4. Where does the error message go in a REPEAT ... UNTIL validation loop?
A. After the loop, so it is shown once the value is valid
B. Inside the loop in an IF that runs when the value is invalid ✓
The error message is only useful while the data is wrong. Put an IF inside the loop to OUTPUT the message when the check fails. After the loop the value is already valid.
5. Which UNTIL condition ends the loop for a Age that must be 11 to 18?
A. UNTIL Age >= 11 AND Age <= 18 ✓
B. UNTIL Age < 11 OR Age > 18
The UNTIL condition is the valid range. AND means both parts must be true, so the age is in range. The wrong answer is true for invalid ages, so it would stop the loop on bad data.

Match the Pairs

REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120 → Keeps asking for Age until the value is inside the valid range 0 to 120, then the loop ends
IF Age < 0 OR Age > 120 THEN OUTPUT "Invalid age" ENDIF → Shows an error message inside the loop when the value fails the validation check
A WHILE version asks for the first input before the loop, then repeats the INPUT while the value is invalid → INPUT Mark WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
A REPEAT loop always runs the prompt and INPUT at least once, and UNTIL checks the value is valid → REPEAT OUTPUT "Enter 1 to 10" INPUT Num UNTIL Num >= 1 AND Num <= 10
REPEAT INPUT Age UNTIL Age < 0 OR Age > 120 → The UNTIL condition describes invalid data, so the loop stops on an invalid age and keeps asking for valid ones. It should be UNTIL Age >= 0 AND Age <= 120.
WHILE Num < 1 OR Num > 10 DO INPUT Num ENDWHILE → There is no INPUT before the WHILE loop, so Num has no value to check the first time. Add INPUT Num before the loop.

Key Term

REPEAT INPUT UNTIL valid
A REPEAT loop around INPUT that keeps asking for a value and shows an error message each time the check fails. The UNTIL condition is the valid range, so the loop ends only on valid data.

Fill in the Blanks

1. REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 100
Hint: Type the command that asks the user to enter a value each time round the loop.
2. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Hint: Type the keyword that ends a REPEAT loop and holds the condition for stopping.
3. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Hint: Type the logical operator that means both parts of the valid range must be true.
4. WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
Hint: Type the logical operator that makes the loop repeat when the value is outside either end of the range.
5. IF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
Hint: Type the command that shows the error message to the user.

Spot the Error

Question 1
A REPEAT
B INPUT Num
C IF Num < 1 AND Num > 10 THEN
D OUTPUT "Error"
E ENDIF
F UNTIL Num >= 1 AND Num <= 10
Line C — A number cannot be below 1 and above 10 at the same time, so the error message would never show. Invalid data is below the range OR above it: IF Num < 1 OR Num > 10 THEN.
Question 2
A INPUT Pts
B WHILE Pts < 0 OR Pts > 50 DO
C OUTPUT "Invalid"
D OUTPUT Pts
E ENDWHILE
Line D — Inside the WHILE loop the program must ask again with INPUT Pts. Outputting the old value never changes Pts, so the loop would never end.
Question 3
A REPEAT
B INPUT Points
C IF Pts < 0 OR Pts > 50 THEN
D OUTPUT "Out of range"
E ENDIF
F UNTIL Pts >= 0 AND Pts <= 50
Line B — The value is read into a different variable (Points) from the one the check and UNTIL use (Pts). It should be INPUT Pts.
Question 4
A REPEAT
B INPUT Num
C UNTIL Num < 1 OR Num > 5
D OUTPUT "Accepted"
Line C — The UNTIL condition must describe VALID data, so the loop stops only when the value is in range. This version stops on invalid data. It should be UNTIL Num >= 1 AND Num <= 5.
Question 5
A WHILE
B INPUT Qty
C IF Qty < 1 OR Qty > 20 THEN
D OUTPUT "Enter 1 to 20"
E ENDIF
F UNTIL Qty >= 1 AND Qty <= 20
Line A — The loop ends with UNTIL, so it must start with REPEAT. WHILE needs a condition and DO, and finishes with ENDWHILE.

Student with a class code? Join your class →