← QCE Digital Solutions Unit 2

Pseudocode: validating a record before insertion

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.

Understanding Pseudocode: validating a record before insertion in QCE Digital Solutions Unit 2

What it does: The check tests one field. When it fails, it changes the valid flag to false, and the record is then blocked. All checks must pass. One passing check does not prove the whole record is good. Inside a condition, = compares the flag to true. <- is for assignment, not comparison. Bad data should not enter the database. Skipping or reporting it protects data integrity. If the flag is not reset for each record, one bad record would cause every later record to be rejected. Validation comes first so the decision to insert is based on all of the checks, not just some of them.

Common mistakes: 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. 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.

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 234–285) and word search (Modules 234–409).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A record has three fields to check before insertion. When should the record be inserted?
Only if every check passes and the valid flag is still trueAs soon as the first check passes
Matches
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.
Applying
Meanings
Term
if valid = true then
Meaning
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.
Practice
valid ← true
Testing
Gaps
valid ←
Check Answer
Spots
1valid <- true
2if age > 0 then

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

The 'Giant Isopod' badge — Patient Builder, 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. 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
All checks must pass. One passing check does not prove the whole record is good.
2. Which line sets up a valid flag before the checks begin?
A. valid <- true ✓
B. valid = true <- if
Pseudocode assigns with <-. The flag starts as true and any failed check changes it to false.
3. A check finds that the age field is -5. What should the pseudocode do with the flag?
A. valid <- true
B. valid <- false ✓
A failed check sets the flag to false so the record will not be inserted.
4. Which is the correct way to guard the insertion?
A. if valid = true then INSERT record ✓
B. if valid <- true then INSERT record
Inside a condition, = compares the flag to true. <- is for assignment, not comparison.
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 ✓
Bad data should not enter the database. Skipping or reporting it protects data integrity.

Match the Pairs

valid <- true → Start by assuming the record is valid. Each failing check will later change this flag to false.
if valid = true then INSERT record → Insert the record only if every validation check left the valid flag true.
A failed check sets the flag to false so the record will not be inserted → if age < 0 then valid <- false
A record with a missing (null) field is rejected as invalid and reported instead of inserted → if name = null then valid <- false
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.
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.

Key Term

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.

Fill in the Blanks

1. valid ← true if age < 0 then valid ← false
Hint: Type the Boolean value the flag holds before any check has failed.
2. if age < 0 or age > 120 then valid ← false
Hint: Type the Boolean operator that makes the check fail if either bad range is true.
3. if valid = true then insert record into students
Hint: Type the command that adds the record to the table once every check has passed.
4. if name = null then valid ← false
Hint: Type the keyword that means a field has no value.
5. if valid = true then insert record into students else print "Record skipped"
Hint: Type the keyword that reports a bad record to the user instead of inserting it.

Spot the 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
Line 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.
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
Line 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.
Question 3
A if valid = false then
B insert record
C else
D print 'Record skipped'
E end if
Line 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.
Question 4
A valid <- true
B if mark < 0 then
C valid = false
D end if
E if valid = true then
F insert record
Line C — In pseudocode, assignment uses the arrow <-. Writing = compares values and does not change the flag. It should be: valid <- false.
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
Line 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.

Student with a class code? Join your class →