Identifying Errors
Errors can be identified in algorithms using trace tables as well as scanning and debugging code manually
Two types of errors are as follows:
- Syntax error
- Syntax refers to the grammar of language, that is, are the words in the right order and is everything spelled correctly
- Unlike programs written on computers, syntax errors in flowcharts and pseudocode do not cause the algorithm to fail however they do make the code more difficult to read and understand
- Transcribing flowcharts and pseudocode to real programming code may cause problems if it is difficult to read and understand said flowcharts and pseudocode
- Logical error
- Logical errors occur when the program finishes but produces the wrong output during or after
- Flowcharts and pseudocode, unlike real programs, do not crash when dry run, however may still produce logic errors by producing incorrect output
- Logical errors are the most difficult error to fix as developers must reconsider their logic and processes. If they do not understand the problem they cannot produce an accurate solution
- Below is an algorithm that asks the user to enter their age to determine what rated movie they could watch at the cinema
- There are syntax and logical errors with this pseudocode
OUTPUT Age
INPUT “Enter an age”
IF Age > 18
THEN
OUTPUT “You can watch an 18 movie”
ELSE
IF Age > 15
THEN
OUTPUT “You can watch a 15 movie
ELSE
IF Age > 12
THEN
OUTPUT “You can watch a 12 movie”
ELSE
IF Age < 9
THEN
OUTPUT “You can watch a PG movie”
ELSE
OUTPUT “You can watch a U movie”
END IF
ENDIF
ENDIF
ENDIF
- Syntax and logic: OUTPUT Age and INPUT “Enter an age” are both syntax and logical errors
- Syntax: Age does not yet have a value so OUTPUT cannot display it while INPUT does not have a variable to store the data as strings cannot store data
- Logical: Both are the wrong way around. OUTPUT Age should be OUTPUT “Enter an age” and INPUT “Enter an age” should be INPUT Age
- Syntax: THEN is missing from the first IF statement
- Syntax: A quote is missing from the OUTPUT “You can watch a 15 movie”
- Logic: Age < 9 should be Age > 9 so that it follows the other IF statement logic
- Syntax: ENDIF is missing from the first IF statement
Worked example
- An algorithm has been written in pseudocode to input some numbers. It only outputs any numbers that are greater than or equal to 100. The number 999 is not output and stops the algorithm.
INPUT Number
WHILE Numbers <> 999 DO
IF Number > 100
THEN
OUTPUT Number
ENDIF
ENDWHILE
OUTPUT Number
(a) Identify the four errors in the pseudocode and suggest corrections.
[4]
Numbers should be Number [1]
IF Number > 100 should be IF Number >= 100 [1]
INPUT Number is missing from inside the loop, insert INPUT Number after the ENDIF statement [1]
The final OUTPUT Number is not needed, remove it [1]
(b) Write a pseudocode statement to change the corrected algorithm to output all numbers between 100 and 200 inclusive. You do not need to rewrite the whole algorithm
[2]
[1] for both ends of the range and correct inequality symbols
[1] for the AND
IF Number >= 100 AND Number <= 200
THEN
OUTPUT Number
ENDIF