Why input data is validated

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Why input data is validated

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
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
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

validation

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
IF Age < 0 __ Age > 120 THEN OUTPUT "Invalid age" ENDIF
2.
REPEAT INPUT Mark UNTIL Mark >= 0 ___ Mark <= 100
3.
IF Age > 120 THEN ______ "Age is too high" ENDIF
4.
WHILE Score > 100 DO INPUT Score ________
5.
IF ______(Password) < 8 THEN OUTPUT "Password too short" ENDIF

Spot the Error

Shade in the letter of the line that contains an 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
Question 2
A // Validation accepts bad data
B INPUT Height
C IF Height < 0 THEN
D OUTPUT "Enter 0 or more"
E ENDIF
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
Question 4
A INPUT Name
B // Reject empty input
C WHILE Name <> "" DO
D OUTPUT "Name is required"
E INPUT Name
F ENDWHILE
Question 5
A INPUT Code
B IF LENGTH(Code) = 4 THEN
C OUTPUT "Need 4 characters"
D ENDIF

Answer Key

Why input data is validated
Section A: Quick Questions
Q1
B — To reject unreasonable or wrongly formed data before it is used
Q2
A — The program, automatically, when the data is input
Q3
A — Reject 450 and show an error message
Q4
A — It is rejected and the user is asked to enter it again
Q5
B — Validation can reject 0 so the program does not crash
Section B: Match the Pairs
Q1
IF Age < 0 OR Age > 120 THEN → A validation check that rejects an unreasonable age before it is used
Q2
OUTPUT "Invalid input, try again" → The error message shown when data fails validation, before the user is asked again
Q3
Validation keeps asking until the data passes the check, so bad data is never used → REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q4
Without validation, bad data gives wrong results, such as an average calculated from an impossible mark → Average ← (Mark1 + Mark2) / 2 // Mark2 typed as 800
Q5
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
Q6
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
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
IF Age < 0 OR Age > 120 THEN OUTPUT "Invalid age" ENDIF
Q2
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q3
IF Age > 120 THEN OUTPUT "Age is too high" ENDIF
Q4
WHILE Score > 100 DO INPUT Score ENDWHILE
Q5
IF LENGTH(Password) < 8 THEN OUTPUT "Password too short" ENDIF
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
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.