Error Types (SQA National 5 Computing Science): Revision Note
Exam code: X816 75
What are errors?
When writing programs, mistakes can occur that stop them working correctly
You must be able to describe, identify, and exemplify the three main types of error:
Syntax errors
Logic errors
Execution (runtime) errors
You also need to explain the effect each one has on the program
Syntax errors
What is a syntax error?
A syntax error breaks the rules of the programming language and prevents the program from running
Examples include:
Missing brackets, colons, or quotation marks
Spelling mistakes in variable names
Incorrect indentation
Using an operator or keyword incorrectly
Examples
SQA Pseudocode |
|---|
With syntax errors |
|
Corrected version |
|
Python |
|---|
With syntax errors |
|
Corrected version |
|
Examiner Tips and Tricks
Use the phrase “prevents the program from running” when describing syntax errors.
Logic errors
What is a logic error?
A logic error occurs when the program runs but gives the wrong output because the logic is incorrect
Examples include:
Using
<instead of<=Adding instead of subtracting
Wrong formula or variable
Condition written incorrectly
Examples
A program is written to display
You passed!if a student achieves a score of 50 or moreIt should display
You failed!is they achieve a score less than 50
SQA Pseudocode |
|---|
With logic error - If the pass mark is meant to include 50, this will give the wrong result |
|
Corrected version |
|
Python |
|---|
With logic error |
|
Corrected version |
|
Examiner Tips and Tricks
Use the phrase “produces an incorrect output” when describing logic errors
Trace tables can help you find logic errors step by step
Execution (runtime) errors
What is an execution (runtime) error?
An execution (runtime) error happens while the program is running and causes it to crash
Examples include:
Dividing by zero
Using a variable before it is assigned
Accessing an element outside the range of an array
Entering the wrong data type
SQA Pseudocode |
|---|
With execution error - If the user enters |
|
Corrected version - with input validation |
|
Python |
|---|
With execution error |
|
Corrected version - with input validation |
|
Examiner Tips and Tricks
Use the phrase “causes the program to crash” when describing runtime (execution) errors
Summary table
Error type | Description | Example cause | Effect on program | Exam phrase |
|---|---|---|---|---|
Syntax | Breaks programming rules | Missing THEN or brackets | Program won’t run | Prevents the program from running |
Logic | Incorrect algorithm | Wrong condition or operator | Wrong output | Produces an incorrect output |
Execution (runtime) | Error during execution | Divide by zero, index error | Program crashes | Causes the program to crash |
How to find and fix errors
Use trace tables or dry runs to follow program flow
Test with normal, extreme, and exceptional data
Read error messages carefully in your IDE
Worked Example
A program is designed to implement input validation, ensuring a numerical score entered by a user is within the acceptable range of 50 to 100, inclusive. If the score is invalid, the user should be prompted to re-enter the input until the condition is met.
The programmer writes the following pseudocode:
Line 1: RECEIVE score FROM KEYBOARD
Line 2: WHILE score < 50 AND score > 100 DO
Line 3: SEND "Error: Score must be between 50 and 100." TO DISPLAY
Line 4: RECEIVE score FROM KEYBOARD
Line 5: END WHILEA user executes the program and inputs the score 10 at Line 1. The program immediately continues past Line 5, accepting the invalid input, and therefore does not function as intended.
(i) State the specific type of error present in the conditional statement at Line 2.
[1]
(ii) Explain why this error occurs, referencing the logical operator used.
[2]
(iii) Rewrite the condition at Line 2 to ensure the input validation works correctly.
[1]
Answers
(i)
The error is a logic error [1 mark]
(ii)
The logical operator AND is used where OR is required [1 mark]
A score can never be simultaneously less than 50 AND greater than 100 [1 mark]
Therefore, the condition
score < 50 AND score > 100is always False, meaning the conditionalWHILEloop never executes, even when invalid exceptional data (like 10) is entered
(iii)
WHILE score < 50 OR score > 100 DO[1 mark]
Unlock more, it's free!
Was this revision note helpful?