Pseudocode: validating a record before insertion

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

Pseudocode: validating a record before insertion

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A record has three fields to check before insertion. When should the record be inserted?
A Only if every check passes and the valid flag is still true
B As soon as the first check passes
2. Which line sets up a valid flag before the checks begin?
A valid <- true
B valid = true <- if
3. A check finds that the age field is -5. What should the pseudocode do with the flag?
A valid <- true
B valid <- false
4. Which is the correct way to guard the insertion?
A if valid = true then INSERT record
B if valid <- true then INSERT record
5. What is a sensible action for a record that fails validation?
A Insert it anyway and fix it later
B Skip it, or report an error message about it, and move on to the next record

Match the Pairs

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

Left columnRight column
1valid <- true AThe else branch resets valid to true, which wipes out a failure from an earlier check. Set valid to true once at the start and only ever change it to false.
2if valid = true then INSERT record BInsert the record only if every validation check left the valid flag true.
3A failed check sets the flag to false so the record will not be inserted CThe record is inserted before the check runs, so a bad record is inserted anyway. The INSERT must come after the checks and sit inside if valid = true then.
4A record with a missing (null) field is rejected as invalid and reported instead of inserted Dif name = null then valid <- false
5valid <- true INSERT record if age < 0 then valid <- false EStart by assuming the record is valid. Each failing check will later change this flag to false.
6if age < 0 then valid <- false else valid <- true Fif age < 0 then valid <- false

Definitions

if valid = true then

Fill in the Blanks

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

1.
valid ← ____ if age < 0 then valid ← false
2.
if age < 0 __ age > 120 then valid ← false
3.
if valid = true then ______ record into students
4.
if name = ____ then valid ← false
5.
if valid = true then insert record into students else _____ "Record skipped"

Spot the Error

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

Question 1
A valid <- true
B if age > 0 then
C valid <- false
D end if
E if valid = true then
F insert record
Question 2
A valid <- true
B if height <= 0 then
C print 'Bad height'
D valid <- true
E end if
F if valid then insert record
Question 3
A if valid = false then
B insert record
C else
D print 'Record skipped'
E end if
Question 4
A valid <- true
B if mark < 0 then
C valid = false
D end if
E if valid = true then
F insert record
Question 5
A valid <- false
B if dob = null then
C valid <- false
D print 'DOB is missing'
E end if
F if valid then insert record

Answer Key

Pseudocode: validating a record before insertion
Section A: Quick Questions
Q1
A — Only if every check passes and the valid flag is still true
Q2
A — valid <- true
Q3
B — valid <- false
Q4
A — if valid = true then INSERT record
Q5
B — Skip it, or report an error message about it, and move on to the next record
Section B: Match the Pairs
Q1
valid <- true → Start by assuming the record is valid. Each failing check will later change this flag to false.
Q2
if valid = true then INSERT record → Insert the record only if every validation check left the valid flag true.
Q3
A failed check sets the flag to false so the record will not be inserted → if age < 0 then valid <- false
Q4
A record with a missing (null) field is rejected as invalid and reported instead of inserted → if name = null then valid <- false
Q5
valid <- true INSERT record if age < 0 then valid <- false → The record is inserted before the check runs, so a bad record is inserted anyway. The INSERT must come after the checks and sit inside if valid = true then.
Q6
if age < 0 then valid <- false else valid <- true → The else branch resets valid to true, which wipes out a failure from an earlier check. Set valid to true once at the start and only ever change it to false.
Section C: Definitions
Q1
if valid = true then — Only insert the record if every validation check left the valid flag true. If any check set valid to false, skip the bad record or report it instead of inserting it.
Section D: Fill in the Blanks
Q1
valid ← true if age < 0 then valid ← false
Q2
if age < 0 or age > 120 then valid ← false
Q3
if valid = true then insert record into students
Q4
if name = null then valid ← false
Q5
if valid = true then insert record into students else print "Record skipped"
Section E: Spot the Error
Q1
B — The age check is reversed. It flags every positive age as invalid and lets negative ages through. A bad age is one that is less than 0, so it should be: if age < 0 then.
Q2
D — After reporting the bad height, the check must set the flag to false so the record is not inserted. Setting it to true leaves a bad record marked as valid. It should be: valid <- false.
Q3
A — The record should only be inserted when the valid flag is true. Testing for false inserts the bad records and skips the good ones. It should be: if valid = true then.
Q4
C — In pseudocode, assignment uses the arrow <-. Writing = compares values and does not change the flag. It should be: valid <- false.
Q5
A — The flag must start as true and only be changed to false when a check fails. If it starts as false, every record is rejected even when all checks pass. It should be: valid <- true.