← IGCSE Computer Science Paper 2

Why input data is validated

An automatic check by the program that input data is reasonable and in the right form. Bad data is rejected with an error message and the user is asked again, so it is never used.

Understanding Why input data is validated in IGCSE Computer Science Paper 2

What it does: The loop repeats the input until the mark is within the valid range, so only reasonable data gets through. The program calculates with the impossible value 800 and produces a wrong average, which validation would have prevented. Validation checks that data is reasonable and in the right form. It cannot know what the user really meant, which is why verification is a separate job. 450 is outside the allowed range, so it is unreasonable. Validation rejects it, shows an error message and the user is asked again. Dividing by zero can crash a program. Validating the input stops this by rejecting the bad value before it is used.

Common mistakes: The age must be read in with INPUT so it can be validated. OUTPUT Age only displays a value and never gets the user's data. It should be INPUT Age. Validation rejects data that is unreasonable or in the wrong form so it is never used. It does not accept bad data. The comment should say rejects.

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 47–57) 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
Why does a program validate input data?
To reject unreasonable or wrongly formed data before it is usedTo prove the data matches what the user meant to type
Matches
INPUT Quantity Total ← Price * Quantity // Quantity is used without any check
↓
The input is used straight away with no validation, so a negative or silly quantity gives a wrong total
Applying
Meanings
Term
validation
Meaning
An automatic check by the program that input data is reasonable and in the right form. Bad data is rejected with an error message and the user is asked again, so it is never used.
Practice
OUTPUT "Enter your age (0 to 120)"
Testing
Gaps
IF Age < 0 Age > 120 THEN
Check Answer
Spots
1OUTPUT "Enter your age"
2OUTPUT Age

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

The 'Roadrunner' badge — Streak Runner, 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. Why does a program validate input data?
A. To prove the data matches what the user meant to type
B. To reject unreasonable or wrongly formed data before it is used ✓
Validation checks that data is reasonable and in the right form. It cannot know what the user really meant, which is why verification is a separate job.
2. Who carries out validation?
A. The program, automatically, when the data is input ✓
B. The user, by reading the data again after typing it
Validation is an automatic check made by the program. A user re-reading their own entry is a visual check, which is verification.
3. A program asks for an exam mark from 0 to 100 and a user types 450. What should validation do?
A. Reject 450 and show an error message ✓
B. Accept 450 because it is a number
450 is outside the allowed range, so it is unreasonable. Validation rejects it, shows an error message and the user is asked again.
4. What happens to data that fails a validation check?
A. It is rejected and the user is asked to enter it again ✓
B. It is used in the program anyway and corrected later
Rejected data is never used. The program shows an error message and asks again until the input is valid.
5. A program divides by a number the user types, and the user types 0. Which statement is true?
A. Validation is not needed because the program will cope
B. Validation can reject 0 so the program does not crash ✓
Dividing by zero can crash a program. Validating the input stops this by rejecting the bad value before it is used.

Match the Pairs

IF Age < 0 OR Age > 120 THEN → A validation check that rejects an unreasonable age before it is used
OUTPUT "Invalid input, try again" → The error message shown when data fails validation, before the user is asked again
Validation keeps asking until the data passes the check, so bad data is never used → REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Without validation, bad data gives wrong results, such as an average calculated from an impossible mark → Average ← (Mark1 + Mark2) / 2 // Mark2 typed as 800
INPUT Quantity Total ← Price * Quantity // Quantity is used without any check → The input is used straight away with no validation, so a negative or silly quantity gives a wrong total
IF Age < 0 THEN OUTPUT "Error" ENDIF // the program then carries on → The error message is shown but the user is not asked again, so the invalid data is still used

Key Term

validation
An automatic check by the program that input data is reasonable and in the right form. Bad data is rejected with an error message and the user is asked again, so it is never used.

Fill in the Blanks

1. IF Age < 0 OR Age > 120 THEN OUTPUT "Invalid age" ENDIF
Hint: Type the logical operator that rejects the age if either unreasonable condition is true.
2. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Hint: Type the logical operator that makes the data valid only when both range conditions are true.
3. IF Age > 120 THEN OUTPUT "Age is too high" ENDIF
Hint: Type the command that shows an error message to the user when the data is rejected.
4. WHILE Score > 100 DO INPUT Score ENDWHILE
Hint: Type the keyword that closes the loop which keeps asking the user for the input again.
5. IF LENGTH(Password) < 8 THEN OUTPUT "Password too short" ENDIF
Hint: Type the function that counts the characters in a string, used in a length check.

Spot the Error

Question 1
A OUTPUT "Enter your age"
B OUTPUT Age
C WHILE Age < 0 OR Age > 120 DO
D OUTPUT "Invalid age"
E INPUT Age
F ENDWHILE
Line B — The age must be read in with INPUT so it can be validated. OUTPUT Age only displays a value and never gets the user's data. It should be INPUT Age.
Question 2
A // Validation accepts bad data
B INPUT Height
C IF Height < 0 THEN
D OUTPUT "Enter 0 or more"
E ENDIF
Line A — Validation rejects data that is unreasonable or in the wrong form so it is never used. It does not accept bad data. The comment should say rejects.
Question 3
A INPUT Qty
B // Check the quantity is in range
C IF Qty < 1 AND Qty > 10 THEN
D OUTPUT "Enter 1 to 10"
E ENDIF
Line C — No number can be below 1 AND above 10 at the same time, so the condition is never true and bad data is never caught. A range check needs OR: IF Qty < 1 OR Qty > 10 THEN.
Question 4
A INPUT Name
B // Reject empty input
C WHILE Name <> "" DO
D OUTPUT "Name is required"
E INPUT Name
F ENDWHILE
Line C — A presence check must loop only when the name is empty. With <> the loop runs for valid names and skips empty ones. It should be WHILE Name = "" DO.
Question 5
A INPUT Code
B IF LENGTH(Code) = 4 THEN
C OUTPUT "Need 4 characters"
D ENDIF
Line B — The error message must show only when the length is wrong. Using = 4 shows the error for valid codes and lets wrong lengths through. It should be IF LENGTH(Code) <> 4 THEN.

Student with a class code? Join your class →