Pseudocode for validating input

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

Pseudocode for validating input

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. A validation loop repeats while the input is...
A valid
B invalid
3. Which condition is a presence check for a variable called name?
A while name = ""
B while name > 100
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
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

Match the Pairs

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

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

Definitions

while input is invalid

Fill in the Blanks

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

1.
while age < 1 __ age > 120 print "Age must be 1-120" input age
2.
while LENGTH(name) = _ print "Name cannot be blank" input name
3.
while mark < 0 OR mark > 100 _____ "Mark must be 0-100" input mark
4.
while score < 0 OR score > 10 print "Enter a score from 0 to 10" _____ score
5.
while LENGTH(pin) _ 4 print "PIN must be 4 digits" input pin

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A input age
B while age < 1 and age > 120
C print "Invalid age"
D input age
E end while
Question 2
A print "Enter name"
B input name
C while name <> ""
D print "Name required"
E input name
F end while
Question 3
A print score
B while score < 0 or score > 100
C print "Enter 0 to 100"
D input score
E end while
Question 4
A input level
B while level < 1 or level > 10
C print "Enter 1 to 10"
D print level
E end while
Question 5
A input password
B while length(password) > 8
C print "Min 8 characters"
D input password
E end while

Answer Key

Pseudocode for validating input
Section A: Quick Questions
Q1
A — input age while age < 12 OR age > 18 print "Age must be 12-18" input age end while
Q2
B — invalid
Q3
A — while name = ""
Q4
B — print "Invalid, try again" input score
Q5
A — So the user can enter a new value and the condition can be tested again
Section B: Match the Pairs
Q1
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
Q2
WHILE name = "" → A presence check condition: the loop repeats while nothing has been entered
Q3
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
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
while age < 1 OR age > 120 print "Age must be 1-120" input age
Q2
while LENGTH(name) = 0 print "Name cannot be blank" input name
Q3
while mark < 0 OR mark > 100 print "Mark must be 0-100" input mark
Q4
while score < 0 OR score > 10 print "Enter a score from 0 to 10" input score
Q5
while LENGTH(pin) ≠ 4 print "PIN must be 4 digits" input pin
Section E: Spot the Error
Q1
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.
Q2
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 = ""
Q3
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.
Q4
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.
Q5
B — The length check should repeat while the password is too short (length < 8). Using > rejects long passwords and accepts short ones.