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