Standard Algorithms (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Input validation

What is input validation?

  • Input validation is a standard algorithm used to check that user input is acceptable

  • It ensures the data entered matches the rules required by the program

  • Students must be able to describe, exemplify, and implement this algorithm using a conditional loop such as WHILE or REPEAT…UNTIL

  • The algorithm should:

    • include a loop condition that continues until valid data is entered

    • include input inside the loop

    • display an error message inside the loop

Example

SQA Pseudocode

Python Equivalent

DECLARE age : INTEGER

REPEAT
    RECEIVE age FROM (INTEGER) KEYBOARD
    IF age < 0 OR age > 120 THEN
        SEND "Please enter a valid age between 0 and 120" TO DISPLAY
    END IF
UNTIL age >= 0 AND age <= 120
age = int(input("Enter your age: "))

while age < 0 or age > 120:
    print("Please enter a valid age between 0 and 120")
    age = int(input("Enter your age: "))

print("Thank you! Your age has been recorded as", age)
  • This example uses a post-conditional loop (REPEAT…UNTIL) so that the program always asks for input at least once

  • You could also use a pre-conditional loop (WHILE) if the input is read before the loop starts, both are valid as long as the logic is correct

  • The while loop is conditional, it continues while the input is invalid

  • The error message and new input are both inside the loop

  • The loop stops only when the input is within the valid range (0–120)

  • You may also come across these types of validation checks:

    • Range check – value must fall within a set range

    • Length check – string must be the correct length

    • Presence check – field must not be left empty

    • Format check – must match a pattern (for example, email format)

    • Type check – must be the correct data type

Examiner Tips and Tricks

In SQA National 5 Computing Science, you only need to be able to describe, exemplify, and implement the standard algorithm for input validation, not memorise every possible check type.

Running total

What is a running total?

  • A running total is a standard algorithm used to keep a total that increases each time a new value is added inside a loop

  • To implement it correctly:

    • Initialise a total variable to 0 before the loop starts

    • Add each new value to the total inside the loop

    • Output the total after the loop ends

Example

SQA Pseudocode

Python Equivalent

DECLARE total : INTEGER ← 0

FOR counter ← 1 TO 10 DO
    RECEIVE number FROM (INTEGER) KEYBOARD
    SET total TO total + number
END FOR

SEND "The total is " & total TO DISPLAY
total = 0

for counter in range(1, 11):
    number = int(input("Enter a number: "))
    total = total + number

print("The total is", total)
  • This program starts a total at 0 and then repeats 10 times

  • Each time, it asks the user to enter a number and adds that number to the total

  • After the loop ends, the total of all entered numbers is displayed

  • In Python, it uses a for loop to run 10 times, adds up all the numbers the user enters, and then outputs the total at the end

Examiner Tips and Tricks

  • Marks are awarded for correctly initialising the total, updating it inside the loop, and displaying the result after the loop

  • If the total is not reset before the loop, the algorithm is incorrect

What is counting?

  • A counting algorithm keeps track of how many times a specific condition is true during a loop

  • To implement it correctly:

    • Initialise the count variable to 0 before the loop

    • Increase the count by 1 when the condition is met

    • Output the count after the loop ends

SQA Pseudocode

Python Equivalent

DECLARE count : INTEGER ← 0

FOR counter ← 1 TO 10 DO
    RECEIVE number FROM (INTEGER) KEYBOARD
    IF number > 5 THEN
        SET count TO count + 1
    END IF
END FOR

SEND "The count is " & count TO DISPLAY
count = 0

for counter in range(1, 11):
    number = int(input("Enter a number: "))
    if number > 5:
        count = count + 1

print("The count is", count)
  • This program keeps track of how many entered numbers are greater than 5

  • It repeats 10 times, increasing the count by 1 each time the condition is true

  • The final count is displayed after the loop ends

  • In Python, it checks if each entered number is greater than 5, adds 1 to the counter when it is, and prints how many numbers met the condition

Traversing 1-D arrays

What is traversing a 1-D array?

  • Traversing means accessing each element of a one-dimensional array in order, from the first to the last

  • If you’re unsure what an array is, visit the revision note on 1-D arrays to review how arrays are declared, indexed, and updated

Example

SQA Pseudocode

Python Equivalent

DECLARE scores : ARRAY[1:5] OF INTEGER
SET scores[1] ← 12
SET scores[2] ← 10
SET scores[3] ← 5
SET scores[4] ← 2
SET scores[5] ← 8

FOR index ← 1 TO 5 DO
    SEND scores[index] TO DISPLAY
END FOR
scores = [12, 10, 5, 2, 8]

for score in scores:
    print(score)
  • This algorithm uses a fixed loop to move through the array from the first element (scores[1]) to the last (scores[5])

  • Each element is accessed and displayed in order

  • This Python program loops through the list scores, printing each value in turn

  • The for score in scores statement automatically starts at the first element and ends at the last

Examiner Tips and Tricks

  • Marks are awarded for correctly using the loop bounds, the array name, and the index variable

  • Remember: in SQA pseudocode, arrays normally start at index 1, not 0

  • Traversing an array often forms part of other algorithms such as running totals or searching

Worked Example

A luxury car wash service requires a program to process new membership sign-ups. The customer's loyalty tier is determined by the annual subscription cost they select.

The program must ensure that the customer enters a valid annual subscription price. A price is considered valid if it meets one of two criteria:

1. The price is between £150 and £300 inclusive. OR

2. The price is exactly £500.

If the customer enters an invalid price, an error message must be displayed, and they must be prompted to re-enter a price.

Using pseudocode, design an efficient solution for inputting and validating the annual subscription price

[4]

Answer

RECEIVE price FROM KEYBOARD
WHILE (price < 150 OR price > 300) AND price <> 500 DO
  SEND "Error: Price must be between 150 and 300, or exactly 500." TO DISPLAY
  RECEIVE price FROM KEYBOARD
END WHILE
  • Use of a conditional loop (WHILE or REPEAT...UNTIL) is required [1 mark]

  • Includes the input statement(s) inside the loop structure [1 mark]

  • Includes an error message and a prompt to re-enter the input inside the conditional loop body [1 mark]

  • The condition must correctly capture the required invalid input range using appropriate logical operators (e.g., checking if the input is not valid, or checking if it is outside the required ranges using multiple AND/OR combinations) [1 mark]

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.