← IGCSE Computer Science Paper 2

Validating input with a REPEAT loop

A validation loop: it asks for INPUT again and again until the value passes the check (here a range check), so invalid data never enters the algorithm.

Understanding Validating input with a REPEAT loop in IGCSE Computer Science Paper 2

What it does: The IF inside the loop outputs an error message for each invalid value, and the UNTIL condition only ends the loop when the value is valid. A length check uses LENGTH(Password) to count the characters. Comparing the text itself to 8 does not check its length. The REPEAT loop re-reads the input until the UNTIL range check is true, which is validation of the input. Valid marks must be at least 0 and at most 100 at the same time, so the two tests need AND to be joined.

Common mistakes: With OR the condition is true for every number, so the loop stops straight away and invalid values get through. A range check needs AND: UNTIL Num >= 1 AND Num <= 10. The loop body must ask for the value again with INPUT. OUTPUT never reads a new Age, so the check cannot be passed. Line 2 should be INPUT Age.

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 254–264) and word search (Modules 254–274).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which loop is the natural choice to keep asking for a mark until it is between 0 and 100?
FOR Mark ← 0 TO 100 INPUT Mark NEXT MarkREPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Matches
REPEAT INPUT Mark UNTIL Mark < 0 OR Mark > 100
↓
The UNTIL condition tests for invalid values, so the loop stops as soon as the mark is wrong. It should be UNTIL Mark >= 0 AND Mark <= 100.
Applying
Meanings
Term
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Meaning
A validation loop: it asks for INPUT again and again until the value passes the check (here a range check), so invalid data never enters the algorithm.
Practice
REPEAT
Testing
Gaps
Check Answer
Spots
2 INPUT Num
3UNTIL Num >= 1 OR Num <= 10

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

The 'Arctic Fox' badge — Tundra Tracker, 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 loop is the natural choice to keep asking for a mark until it is between 0 and 100?
A. FOR Mark ← 0 TO 100 INPUT Mark NEXT Mark
B. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 ✓
A validation loop must always ask at least once and then repeat until the value passes the check. REPEAT ... UNTIL does this.
2. Which condition correctly ends the loop once a valid mark (0 to 100) has been entered?
A. UNTIL Mark >= 0 AND Mark <= 100 ✓
B. UNTIL Mark >= 0 OR Mark <= 100
REPEAT ... UNTIL stops when the condition is TRUE. A valid mark must be both at least 0 AND at most 100. With OR, almost every number would pass, even 500 or -20.
3. A student types 150 as a mark. What does REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 do?
A. It accepts 150 and carries on
B. It asks for INPUT again ✓
150 is greater than 100, so the UNTIL condition is FALSE and the loop repeats the INPUT.
4. Where should the error message go in a REPEAT validation loop?
A. Inside the loop, in an IF after the INPUT ✓
B. After the UNTIL line, so it only shows once at the end
The message must be shown each time the value is invalid, so it belongs inside the loop. Example: IF Mark < 0 OR Mark > 100 THEN OUTPUT "Enter 0 to 100" ENDIF.
5. Which check does this loop perform? REPEAT INPUT Name UNTIL Name <> ""
A. A presence check ✓
B. A range check
The loop keeps asking until something has been entered, so the value is not empty. That is a presence check.

Match the Pairs

REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120 → Keeps asking for Age until it is in the range 0 to 120, so an invalid age never gets into the algorithm.
REPEAT INPUT Name UNTIL LENGTH(Name) > 0 → Keeps asking for Name until something is typed in, which is a presence check.
A validation loop that shows an error message each time the value is outside the range 1 to 10 → REPEAT INPUT Score IF Score < 1 OR Score > 10 THEN OUTPUT "Enter 1 to 10" ENDIF UNTIL Score >= 1 AND Score <= 10
The WHILE form of a range check: read first, then keep asking while the value is invalid → INPUT Mark WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
REPEAT INPUT Mark UNTIL Mark < 0 OR Mark > 100 → The UNTIL condition tests for invalid values, so the loop stops as soon as the mark is wrong. It should be UNTIL Mark >= 0 AND Mark <= 100.
REPEAT INPUT Mark UNTIL Mark >= 0 OR Mark <= 100 → OR makes the condition true for every number, so the range check never rejects anything. It should use AND.

Key Term

REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
A validation loop: it asks for INPUT again and again until the value passes the check (here a range check), so invalid data never enters the algorithm.

Fill in the Blanks

1. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Hint: Type the keyword that starts a loop whose body always runs at least once before the check.
2. REPEAT INPUT Password UNTIL LENGTH(Password) >= 8
Hint: Type the function that returns how many characters a string holds, used for a length check.
3. IF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
Hint: Type the keyword that shows an error message to the user when the data fails the check.
4. WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
Hint: Type the logical operator that makes the loop repeat when the mark is too low or too high.
5. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Hint: Type the relational operator that checks the mark does not go above the upper limit.

Spot the Error

Question 1
A REPEAT
B INPUT Num
C UNTIL Num >= 1 OR Num <= 10
D OUTPUT Num
Line C — With OR the condition is true for every number, so the loop stops straight away and invalid values get through. A range check needs AND: UNTIL Num >= 1 AND Num <= 10.
Question 2
A REPEAT
B OUTPUT Age
C UNTIL Age >= 5 AND Age <= 18
D OUTPUT Age
Line B — The loop body must ask for the value again with INPUT. OUTPUT never reads a new Age, so the check cannot be passed. Line 2 should be INPUT Age.
Question 3
A INPUT Num
B WHILE Num < 1 AND Num > 10 DO
C OUTPUT "Enter 1 to 10"
D INPUT Num
E ENDWHILE
Line B — No number can be below 1 and above 10 at once, so the loop never runs and invalid data is accepted. The WHILE form repeats while the value is invalid, so it needs OR: WHILE Num < 1 OR Num > 10 DO.
Question 4
A WHILE
B INPUT Num
C UNTIL Num >= 1 AND Num <= 10
Line A — The loop ends with UNTIL, so it must begin with REPEAT. WHILE needs DO and ENDWHILE instead. Line 1 should be REPEAT.
Question 5
A DECLARE Num : STRING
B REPEAT
C INPUT Num
D UNTIL Num >= 1 AND Num <= 10
Line A — A range check compares Num with numbers, so Num must be a numeric type. It should be declared as INTEGER, not STRING.

Student with a class code? Join your class →