Standard Algorithms (SQA National 5 Computing Science): Revision Note
Exam code: X816 75
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
WHILEorREPEAT…UNTILThe 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 |
|---|---|
| |
|
|
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 |
|---|---|
| |
|
|
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 |
|---|---|
| |
|
|
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 |
|---|---|
| |
|
|
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 WHILEUse of a conditional loop (
WHILEorREPEAT...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/ORcombinations) [1 mark]
Unlock more, it's free!
Did this page help you?