Guide 7 of 7

Robustness & Validation

Never trust the user. Learn how to write bulletproof code using Validation loops, Range checks, and Presence checks to prevent your algorithms from crashing.

Exam Strategy: Verification vs. Validation

Cambridge examiners frequently ask students to describe the difference between Verification and Validation. Do not get these mixed up!

Validation: Checking that the data inputted by the user is reasonable and meets certain criteria (e.g., ensuring a test score is between 0 and 100). The computer does this automatically.
Verification: Checking that data has been entered accurately (e.g., asking the user to type their password twice to ensure they didn't make a typo).

Python vs. Pseudocode Validation

In Python, programmers often use an infinite while True: loop combined with a break statement to trap a user until they enter correct data. In Cambridge pseudocode, you must use a REPEAT...UNTIL loop.

Concept 🐍 Python (Wrong for Exams) 📘 Cambridge Pseudocode (Correct)
Validation Loop while True:
  x = int(input())
  if x > 0: break
REPEAT
  INPUT X
UNTIL X > 0

1. The 3 Core Validation Checks

You need to memorize the code for these three specific validation methods:

Range Check: Ensures a number falls between a lower and upper boundary.
IF Score >= 0 AND Score <= 100 THEN...
Length Check: Ensures a string has the correct number of characters.
IF LENGTH(Password) > 8 THEN...
Presence Check: Ensures the user didn't just hit 'Enter' and leave it blank.
IF Name <> "" THEN...

2. Writing a Validation Loop

A true robust program doesn't just crash when the user enters bad data; it traps the user in a loop until they get it right. You should always use the REPEAT...UNTIL structure for this.

Validation Loop Syntax
DECLARE Choice : INTEGER

// The user is trapped until they enter 1 or 2
REPEAT
    OUTPUT "Enter 1 to Start, 2 to Quit:"
    INPUT Choice
    
    IF Choice <> 1 AND Choice <> 2 THEN
        OUTPUT "Error: Invalid choice!"
    ENDIF
    
UNTIL Choice = 1 OR Choice = 2

OUTPUT "Proceeding with option ", Choice

Interactive Exam Practice

Scenario: Write an algorithm that asks a teacher to input a student's test score. Implement a Range Check validation loop to ensure the score is between 0 and 100 inclusive. Keep asking until a valid score is provided.

Hover or tap the black box to reveal the examiner's solution. Click Run Code to test it instantly!

Tap to Reveal Answer
DECLARE Mark : INTEGER

REPEAT
    OUTPUT "Enter a valid mark (0 to 100):"
    INPUT Mark
UNTIL Mark >= 0 AND Mark <= 100

OUTPUT "Score accepted and recorded."