Free revision guides

Input validation: how to get all 4 marks

National 5 Computing Science: Software design and development

Input validation is one of the two algorithms that come up in the National 5 Computing Science exam almost every year, usually for 4 marks. The examiners' reports for 2023, 2024 and 2025 all point to the same mistake.

What input validation does

The program asks for a value, and keeps asking until the value is acceptable. An age must be from 0 to 120; a menu choice must be A, B or C; a password must be at least 8 characters.

The mistake: checking with IF

RECEIVE age FROM (INTEGER) KEYBOARD
IF age < 0 OR age > 120 THEN
   SEND "Age must be 0 to 120" TO DISPLAY
   RECEIVE age FROM (INTEGER) KEYBOARD
END IF

This checks the age only once. If the user types a wrong value a second time, the program accepts it. An IF is a decision, not a repeat.

The version that gains the marks: a conditional loop

RECEIVE age FROM (INTEGER) KEYBOARD
WHILE age < 0 OR age > 120 DO
   SEND "Age must be 0 to 120. Please try again." TO DISPLAY
   RECEIVE age FROM (INTEGER) KEYBOARD
END WHILE

The loop keeps going while the value is wrong, however many times that takes. In Python the same idea looks like this:

age = int(input("Age: "))
while age < 0 or age > 120:
    print("Age must be 0 to 120. Please try again.")
    age = int(input("Age: "))
What the marker looks for
  1. A conditional loop (WHILE, or REPEAT ... UNTIL), not IF.
  2. The right condition: it describes a wrong value, so the loop runs while the value is wrong.
  3. The input repeated inside the loop, so the user gets another go.
  4. An error message inside the loop.

Two more traps

  • AND instead of OR. WHILE age < 0 AND age > 120 can never be true, because no number is both below 0 and above 120. The loop never runs, so nothing is ever rejected.
  • It is not always a number range. The question might want a code exactly 5 characters long (use length(code) != 5), or one of a set of choices (choice != "A" AND choice != "B" AND choice != "C"). Notice that "not one of these" needs AND.

Try two exam-style questions

Choose an answer, say how sure you are, then press Check. Every wrong answer tells you why it is wrong.

The Standard algorithms simulation
In the course: Standard algorithms. Step through input validation, running totals and traversing an array, one line at a time, and predict what happens next.

The full course covers every topic like this. Every topic in National 5 Computing Science has a short note, a simulation to try and a practice quiz with feedback on every answer, plus coding lessons, a full mock exam and a progress report for parents.

Create an accountTry more for freeAbout the course

More guides

This site is independent: it is not connected to the SQA or Qualifications Scotland. Points from the examiners' course reports are given in our own words.