← QCE Digital Solutions Unit 3

Pseudocode for validating input

A loop that reads input, tests it (presence, type, range or length), then shows an error message and asks again until the input passes the check.

Understanding Pseudocode for validating input in QCE Digital Solutions Unit 3

What it does: The loop body prints an error message and re-reads the input, so the check is repeated until the score is valid. An age is invalid if it is below 12 OR above 18. No number can be both below 12 and above 18, so AND would never repeat the loop. A presence check tests that something was entered. An empty string means the user left the input blank. Comparing to 100 is a range check, not a presence check.

Common mistakes: No age can be below 1 and above 120 at once, so the loop never runs and bad input gets through. Use 'or' so the loop repeats when the age is outside the range. A presence check should loop while the name is empty (name = ""). With <> the loop runs when a name has been entered, and it stops when the input is blank. It should be: while name = "".

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 194–250) and word search (Modules 194–337).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which pseudocode correctly keeps asking until the age is in the range 12 to 18?
input age while age < 12 OR age > 18 print "Age must be 12-18" input age end whileinput age while age < 12 AND age > 18 print "Age must be 12-18" input age end while
Matches
input age WHILE age < 12 OR age > 18 print "Age must be 12 to 18" ENDWHILE
↓
The loop never asks for input again, so age never changes and the loop repeats forever after an invalid entry.
Applying
Meanings
Term
while input is invalid
Meaning
A loop that reads input, tests it (presence, type, range or length), then shows an error message and asks again until the input passes the check.
Practice
print "Enter your age (5 to 18):"
Testing
Gaps
while age < 1 age > 120
Check Answer
Spots
1input age
2while age < 1 and age > 120

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

The 'Owl' badge — Wise 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. Which pseudocode correctly keeps asking until the age is in the range 12 to 18?
A. input age while age < 12 OR age > 18 print "Age must be 12-18" input age end while ✓
B. input age while age < 12 AND age > 18 print "Age must be 12-18" input age end while
An age is invalid if it is below 12 OR above 18. No number can be both below 12 and above 18, so AND would never repeat the loop.
2. A validation loop repeats while the input is...
A. valid
B. invalid ✓
The loop keeps asking while the input fails the check. Once the input is valid, the loop ends and the program moves on.
3. Which condition is a presence check for a variable called name?
A. while name = "" ✓
B. while name > 100
A presence check tests that something was entered. An empty string means the user left the input blank. Comparing to 100 is a range check, not a presence check.
4. Which loop body is the best way to handle invalid input?
A. input score print "Invalid, try again"
B. print "Invalid, try again" input score ✓
Show the error message first, then ask again. The new input is then tested by the loop condition. In the wrong version the new value is never explained to the user and the message appears after the new input.
5. Why must the loop body include a second input statement?
A. So the user can enter a new value and the condition can be tested again ✓
B. So the program can store the input twice
Without a fresh input inside the loop, the variable never changes. An invalid value would stay invalid and the loop would repeat forever.

Match the Pairs

WHILE age < 12 OR age > 18 → A range check condition: the loop keeps repeating while the age falls outside the allowed range of 12 to 18
WHILE name = "" → A presence check condition: the loop repeats while nothing has been entered
After a failed check, show an error message and ask the user to enter the value again inside the loop → WHILE score < 0 OR score > 100 print "Enter a score from 0 to 100" input score ENDWHILE
Read the first input before the loop so the condition has a value to test → input age WHILE age < 12 OR age > 18 print "Age must be 12 to 18" input age ENDWHILE
input age WHILE age < 12 OR age > 18 print "Age must be 12 to 18" ENDWHILE → The loop never asks for input again, so age never changes and the loop repeats forever after an invalid entry.
input age WHILE age >= 12 AND age <= 18 print "Age must be 12 to 18" input age ENDWHILE → The condition tests for VALID input, so the loop runs and shows an error when the age is correct, but accepts invalid ages.

Key Term

while input is invalid
A loop that reads input, tests it (presence, type, range or length), then shows an error message and asks again until the input passes the check.

Fill in the Blanks

1. while age < 1 OR age > 120 print "Age must be 1-120" input age
Hint: Type the Boolean operator that makes the input invalid if it is outside the range at either end.
2. while LENGTH(name) = 0 print "Name cannot be blank" input name
Hint: Type the number that the length of an empty input equals, which is what a presence check looks for.
3. while mark < 0 OR mark > 100 print "Mark must be 0-100" input mark
Hint: Type the keyword that shows the error message to the user.
4. while score < 0 OR score > 10 print "Enter a score from 0 to 10" input score
Hint: Type the keyword that asks the user to enter the value again.
5. while LENGTH(pin) ≠ 4 print "PIN must be 4 digits" input pin
Hint: Type the comparison operator meaning 'not equal to' so a length check repeats for the wrong length.

Spot the Error

Question 1
A input age
B while age < 1 and age > 120
C print "Invalid age"
D input age
E end while
Line B — No age can be below 1 and above 120 at once, so the loop never runs and bad input gets through. Use 'or' so the loop repeats when the age is outside the range.
Question 2
A print "Enter name"
B input name
C while name <> ""
D print "Name required"
E input name
F end while
Line C — A presence check should loop while the name is empty (name = ""). With <> the loop runs when a name has been entered, and it stops when the input is blank. It should be: while name = ""
Question 3
A print score
B while score < 0 or score > 100
C print "Enter 0 to 100"
D input score
E end while
Line A — The program must read a value with 'input score' before the loop tests it. 'print score' only displays a variable that has no value yet.
Question 4
A input level
B while level < 1 or level > 10
C print "Enter 1 to 10"
D print level
E end while
Line D — After the error message the program must ask again with 'input level'. Printing the old value never changes it, so the loop repeats forever.
Question 5
A input password
B while length(password) > 8
C print "Min 8 characters"
D input password
E end while
Line B — The length check should repeat while the password is too short (length < 8). Using > rejects long passwords and accepts short ones.

Student with a class code? Join your class →