Writing a validated input algorithm in pseudocode

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

Writing a validated input algorithm in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line should come first in a validation algorithm for a variable called Age?
A INPUT Age
B DECLARE Age : INTEGER
2. Which shows the correct order inside a validation loop?
A OUTPUT "Enter age" then INPUT Age
B INPUT Age then OUTPUT "Enter age"
3. A program must accept an age from 1 to 120. Which UNTIL line is correct?
A UNTIL Age >= 1 AND Age <= 120
B UNTIL Age < 1 OR Age > 120
4. Where should the error message be placed in a REPEAT ... UNTIL validation loop?
A After the UNTIL line, so it always runs once at the end
B Inside the loop, in an IF that runs when the check fails
5. Which UNTIL line gives a correct presence check on Name?
A UNTIL Name <> ""
B UNTIL Name = ""

Match the Pairs

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

Left columnRight column
1REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120 AKeeps asking for input until the value passes a range check between 0 and 120
2UNTIL LENGTH(Password) >= 8 BThere is no error message when the value is rejected, and the variable Age is never declared with DECLARE Age : INTEGER before the loop.
3A validation algorithm must first declare the variable that will store the input CDECLARE Score : INTEGER
4A presence check loop repeats until the input is not empty DREPEAT INPUT Name UNTIL Name <> ""
5REPEAT INPUT Mark UNTIL Mark < 0 OR Mark > 100 ELoop exit condition that repeats until the password passes a length check of at least 8 characters
6REPEAT OUTPUT "Enter age" INPUT Age UNTIL Age >= 0 AND Age <= 120 FThe UNTIL condition tests for invalid values, so the loop stops when the mark is wrong. It should be Mark >= 0 AND Mark <= 100.

Definitions

validation loop

Fill in the Blanks

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

1.
_______ Age : INTEGER OUTPUT "Enter your age (1-120)" INPUT Age
2.
DECLARE Age : INTEGER OUTPUT "Enter your age (1-120)" _____ Age
3.
REPEAT INPUT Age UNTIL Age >= 1 ___ Age <= 120
4.
REPEAT INPUT Password UNTIL ______(Password) >= 8
5.
IF Name = "" THEN ______ "Please enter a name" ENDIF

Spot the Error

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

Question 1
A DECLARE Age INTEGER
B REPEAT
C OUTPUT "Enter age (1-99)"
D INPUT Age
E UNTIL Age >= 1 AND Age <= 99
Question 2
A DECLARE Name : STRING
B Repeat
C OUTPUT "Enter your name"
D INPUT Name
E UNTIL Name <> ""
Question 3
A DECLARE Pass : STRING
B REPEAT
C OUTPUT "Enter password"
D READ Pass
E UNTIL LENGTH(Pass) >= 8
Question 4
A DECLARE Mark : INTEGER
B REPEAT
C INPUT "Enter mark (0-10)"
D INPUT Mark
E UNTIL Mark >= 0 AND Mark <= 10
Question 5
A DECLARE Qty : INTEGER
B REPEAT
C OUTPUT Enter quantity
D INPUT Qty
E UNTIL Qty >= 1 AND Qty <= 20

Answer Key

Writing a validated input algorithm in pseudocode
Section A: Quick Questions
Q1
B — DECLARE Age : INTEGER
Q2
A — OUTPUT "Enter age" then INPUT Age
Q3
A — UNTIL Age >= 1 AND Age <= 120
Q4
B — Inside the loop, in an IF that runs when the check fails
Q5
A — UNTIL Name <> ""
Section B: Match the Pairs
Q1
REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120 → Keeps asking for input until the value passes a range check between 0 and 120
Q2
UNTIL LENGTH(Password) >= 8 → Loop exit condition that repeats until the password passes a length check of at least 8 characters
Q3
A validation algorithm must first declare the variable that will store the input → DECLARE Score : INTEGER
Q4
A presence check loop repeats until the input is not empty → REPEAT INPUT Name UNTIL Name <> ""
Q5
REPEAT INPUT Mark UNTIL Mark < 0 OR Mark > 100 → The UNTIL condition tests for invalid values, so the loop stops when the mark is wrong. It should be Mark >= 0 AND Mark <= 100.
Q6
REPEAT OUTPUT "Enter age" INPUT Age UNTIL Age >= 0 AND Age <= 120 → There is no error message when the value is rejected, and the variable Age is never declared with DECLARE Age : INTEGER before the loop.
Section C: Definitions
Q1
validation loop — DECLARE the variable, OUTPUT a prompt, INPUT the value, OUTPUT an error message if it fails the check, and REPEAT ... UNTIL the value is acceptable.
Section D: Fill in the Blanks
Q1
DECLARE Age : INTEGER OUTPUT "Enter your age (1-120)" INPUT Age
Q2
DECLARE Age : INTEGER OUTPUT "Enter your age (1-120)" INPUT Age
Q3
REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 120
Q4
REPEAT INPUT Password UNTIL LENGTH(Password) >= 8
Q5
IF Name = "" THEN OUTPUT "Please enter a name" ENDIF
Section E: Spot the Error
Q1
A — A DECLARE statement needs a colon between the identifier and the data type. It should be DECLARE Age : INTEGER.
Q2
B — Cambridge pseudocode keywords must be written in capital letters. The loop must start with REPEAT, not Repeat.
Q3
D — READ is not a Cambridge pseudocode command. The value must be read with INPUT, so the line should be INPUT Pass.
Q4
C — The prompt for the user must be shown with OUTPUT. INPUT is used to store the value in a variable, so the line should be OUTPUT "Enter mark (0-10)".
Q5
C — The text of a prompt must be inside double quotes, otherwise it looks like identifiers. The line should be OUTPUT "Enter quantity".