7.8

7.8 Identifying & Fixing Errors in Pseudocode

Identify, describe and fix syntax, logic and runtime errors in pseudocode algorithms.

Learning Objectives

By the end of this lesson, you will be able to:

  • Name and describethe three main categories of programming errors
  • Identify and fixsyntax errors in CIE pseudocode
  • Identify and fixlogic errors that cause incorrect output
  • Identify and fixruntime errors that cause a program to crash
  • Usethe correct exam phrases to describe the impact of each error type
  • Applythese skills to write and amend algorithms in CIE pseudocode

Key Terms

Syntax error

An error that breaks the grammatical rules of pseudocode and stops the program from running.

Logic error

Incorrect code that allows the program to run but produces an incorrect output or result.

Runtime error

An error that causes a program to crash while it is running.

Syntax

The grammatical rules of pseudocode — for example, using the correct keywords likeTHENandENDIF.

Trace table

A table that records the value of every variable at every step of an algorithm — useful for finding logic errors.

Dry run

Executing an algorithm manually, step by step, on paper or in a trace table — without using a computer.

1. Types of Errors

Designing algorithms is a skill that must be developed, and when designing algorithms, mistakes and issues will occur.Trace tablescan help to find any kind of error in a program or algorithm.

There arethree main categoriesof errors that a programmer must be able to identify and fix:

Syntax errors

Break the grammatical rules of pseudocode andprevent the program from running.

Logic errors

The program runs butproduces incorrect outputbecause of incorrect code.

Runtime errors

Cause the program tocrashwhile it is running.

Examiner Tips and Tricks

When describing an error, use the exact phrases that earn marks in the exam:

  • Use"prevents the program from running"for asyntaxerror
  • Use"produces incorrect output"for alogicerror
  • Use"causes the program to crash"for aruntimeerror

Examiners won't give marks for vague phrases like "the program didn't work." You mustname the error type,locate it, andexplain the impact.

Activity 1: Match the Error Type

Match each description below to the correct error type (syntax, logic or runtime):

  1. A missingTHENkeyword after anIFstatement
  2. Using<instead of>in a comparison
  3. Dividing a number by zero
  4. Accessing an array index that is out of range
  5. A missing closing quotation mark on a string
  6. A loop that runs one too many times
Solution:
  1. Syntax error— missing keyword prevents the program from running.
  2. Logic error— the program runs but produces incorrect output.
  3. Runtime error— dividing by zero causes the program to crash.
  4. Runtime error— an out-of-range index causes the program to crash.
  5. Syntax error— a missing quote prevents the program from running.
  6. Logic error— the program runs but produces incorrect output.

Check Your Understanding: Types of Errors

Answer
  • [1 mark]Syntax errors
  • [1 mark]Logic errors
  • [1 mark]Runtime errors
Answer
Error typeExam phrase
Syntax error"prevents the program from running"
Logic error"produces incorrect output"
Runtime error"causes the program to crash"
Answer
  • [1 mark]A syntax error stops the program from running, so it is flagged by the compiler or interpreter
  • [1 mark]A logic error allows the program to run with no error message
  • [1 mark]The programmer must trace through the algorithm (e.g. using a trace table) to find the wrong logic
Answer
  • [2 marks]A runtime error is an error that causes the program to crash while it is running
  • [2 marks]Examples (any 2): dividing a number by 0; an index out of the range of an array; unable to read or write to a drive

2. Syntax Errors in Pseudocode

Asyntax erroris an error that breaks the grammatical rules of pseudocode andstops it from running. Examples include typos and spelling errors, missing or extra keywords, missing or extra brackets or quotes, and incorrectly nested blocks of code.

Common syntax errors in CIE pseudocode
  • MissingTHENafter anIFcondition
  • MissingENDIFto close anIFblock
  • MissingNEXTto close aFORloop
  • Missing closing quotation mark on a string
  • Misspelled keywords (e.g.OUTPTinstead ofOUTPUT)
  • MissingDECLAREfor a variable

2.1 Worked Example: Voting Age Program

The pseudocode below is intended to check whether a user is old enough to vote. It contains two syntax errors.

Pseudocode with syntax errors:

// Program to work out if you are old enough to vote
CONSTANT VotingAge ← 18
OUTPUT "How old are you?"
INPUT YourAge
IF (YourAge <= VotingAge)
OUTPUT "You are old enough to vote"
ELSE
OUTPUT "You are not old enough to vote
ENDIF

Syntax Error 1: Missing THEN keyword

The keywordTHENis missing after theIFcondition. In CIE pseudocode, theTHENmust be on the same line or the next line.

Impact:Prevents the program from running.

Syntax Error 2: Missing closing quote

The OUTPUT line"You are not old enough to voteis missing its closing double quote.

Impact:Prevents the program from running.

Corrected pseudocode (syntax fixed):

// Program to work out if you are old enough to vote
CONSTANT VotingAge ← 18
OUTPUT "How old are you?"
INPUT YourAge
IF (YourAge >= VotingAge) THEN
OUTPUT "You are old enough to vote"
ELSE
OUTPUT "You are not old enough to vote"
ENDIF
Key observation

Once the syntax errors are fixed, the program runs.However, the operator<=should have been>=— this is alogic errorthat we'll explore in the next section.

2.2 Another Example: Score Grading

The pseudocode below assigns a grade based on a score. It contains three syntax errors.

DECLARE Score : INTEGER
DECLARE Grade : STRING
OUTPUT "Enter your score: "
INPUT Score
IF Score >= 50
Grade ← "Pass"
ELSE
Grade ← "Fail
OUTPUT "Your grade is: ", Grade

Syntax Error 1: Missing THEN

IF Score >= 50needsTHENat the end.

Syntax Error 2: Missing closing quote

Grade ← "Failneeds a closing quote:Grade ← "Fail".

Syntax Error 3: Missing ENDIF

TheIF…ELSEblock is not closed withENDIF. In CIE pseudocode, everyIFmust be closed withENDIF.

Corrected pseudocode:

DECLARE Score : INTEGER
DECLARE Grade : STRING
OUTPUT "Enter your score: "
INPUT Score
IF Score >= 50 THEN
Grade ← "Pass"
ELSE
Grade ← "Fail"
ENDIF
OUTPUT "Your grade is: ", Grade

Activity 2: Find the Syntax Errors

The pseudocode below outputs the numbers 1 to 5 and their squares. Find the two syntax errors and describe how to fix each one.

DECLARE Count : INTEGER
DECLARE Square : INTEGER
FOR Count ← 1 TO 5
Square ← Count * Count
OUTPUT Count, " squared is ", Square
Solution:
  1. Error:TheFORloop is missingNEXT Countto close it.
    Fix:AddNEXT Countafter the OUTPUT line.
  2. Error:Only one error is shown, but a second one is that the FOR loop should haveNEXT Count— this is required in CIE pseudocode to close the loop.

Corrected code:

DECLARE Count : INTEGER
DECLARE Square : INTEGER
FOR Count ← 1 TO 5
Square ← Count * Count
OUTPUT Count, " squared is ", Square
NEXT Count

Check Your Understanding: Syntax Errors in Pseudocode

Answer
  • [1 mark]A syntax error breaks the grammatical rules of pseudocode
  • [1 mark]It prevents the program from running
Answer
  • [1 mark]MissingTHENafter theIFcondition
  • [1 mark]Missing closing double quote on"You are not old enough to vote
Answer
  • [1 mark]The program will not run at all — it is prevented from running
  • [1 mark]The error must be fixed before the algorithm can be executed or translated into code
Answer
  • [1 mark]MissingTHENafter anIFcondition
  • [1 mark]MissingENDIForNEXTto close a block
  • [1 mark]Missing closing quotation mark on a string / misspelled keywords

3. Logic Errors in Pseudocode

Alogic erroris where incorrect code is used that causes the program to run,but produces an incorrect output or result. Logic errors can be difficult to identify by the person who wrote the program, so one method of finding them is to usetrace tables.

Examples of logic errors
  • Incorrect use of operators (<and>,<=and>=)
  • Logical operator confusion (ANDforOR)
  • Looping one extra time or one time too few
  • Using the wrong variable name
  • Using the wrong mathematical formula
  • Infinite loops

3.1 Voting Age Program — The Logic Error

Now that the syntax errors are fixed, the program runs — but it still doesn't behave as expected. Look carefully at the comparison operator:

// Program to work out if you are old enough to vote
CONSTANT VotingAge ← 18
OUTPUT "How old are you?"
INPUT YourAge
IF (YourAge <= VotingAge) THEN
OUTPUT "You are old enough to vote"
ELSE
OUTPUT "You are not old enough to vote"
ENDIF

Logic Error: Wrong comparison operator

<=will only permit users aged 18 or under to vote — that's the opposite of what the program is supposed to do.

Impact:The program runs but produces incorrect output — it gives the wrong message.

Fix:Change the operator from<=to>=.

Corrected pseudocode (logic fixed):

// Program to work out if you are old enough to vote
CONSTANT VotingAge ← 18
OUTPUT "How old are you?"
INPUT YourAge
IF (YourAge >= VotingAge) THEN
OUTPUT "You are old enough to vote"
ELSE
OUTPUT "You are not old enough to vote"
ENDIF

3.2 Rectangle Area — Testing for Logic Errors

This pseudocode calculates the area of a rectangle. It should reject any length or width that is not positive (i.e. greater than 0). Use the test table below to find the logic error.

DECLARE length : REAL
DECLARE width : REAL
DECLARE area : REAL
OUTPUT "Enter the length: "
INPUT length
OUTPUT "Enter the width: "
INPUT width
IF length < 0 OR width < 0 THEN
OUTPUT "Length and width must be positive values."
ELSE
area ← length * width
OUTPUT "The area is ", area
ENDIF
TestTest dataExpected outcomeActual outcomeChange needed?
1length = 5
width = 5
"The area is 25""The area is 25"N
2length = 10
width = 0
"Length and width must be positive values.""The area is 0"Y — should not accept 0 input as not positive

Logic error located

The error is on the line:IF length < 0 OR width < 0 THEN

Fix:The expression< 0should be<= 0so that 0 is not accepted as valid input for length or width.

Corrected pseudocode (logic fixed):

DECLARE length : REAL
DECLARE width : REAL
DECLARE area : REAL
OUTPUT "Enter the length: "
INPUT length
OUTPUT "Enter the width: "
INPUT width
IF length <= 0 OR width <= 0 THEN
OUTPUT "Length and width must be positive values."
ELSE
area ← length * width
OUTPUT "The area is ", area
ENDIF

3.3 Average of Five Numbers — Logic Error

The pseudocode below is supposed to calculate the average of five numbers. It contains a logic error.

DECLARE total : INTEGER
DECLARE count : INTEGER
DECLARE average : REAL
total ← 0
FOR count ← 1 TO 5
total ← total + count
NEXT count
average ← total / 4
OUTPUT "Average: ", average

Logic Error: Wrong divisor

The total is divided by 4 instead of 5. Since there are 5 numbers, the average should be divided by 5.

Impact:The program runs but produces incorrect output — the average will be too high.

Fix:Changeaverage ← total / 4toaverage ← total / 5.

Corrected pseudocode (logic fixed):

DECLARE total : INTEGER
DECLARE count : INTEGER
DECLARE average : REAL
total ← 0
FOR count ← 1 TO 5
total ← total + count
NEXT count
average ← total / 5
OUTPUT "Average: ", average

Activity 3: Find the Logic Error

The pseudocode below is supposed to output the highest of three numbers. It contains a logic error.

DECLARE a : INTEGER
DECLARE b : INTEGER
DECLARE c : INTEGER
DECLARE highest : INTEGER
INPUT a
INPUT b
INPUT c
highest ← a
IF b < highest THEN
highest ← b
ENDIF
IF c < highest THEN
highest ← c
ENDIF
OUTPUT "Highest is ", highest
  1. Identify the logic error.
  2. Explain the effect it has on the output.
  3. Suggest a fix.
Solution:
  1. Error:The comparison operators are<instead of>. The program finds thesmallestnumber, not the highest.
  2. Effect:The program runs but produces incorrect output — it outputs the lowest number instead of the highest.
  3. Fix:Change both<to>:
    IF b > highest THEN
    highest ← b
    ENDIF
    IF c > highest THEN
    highest ← c
    ENDIF

Check Your Understanding: Logic Errors in Pseudocode

Answer
  • [1 mark]A logic error is incorrect code that allows the program to run
  • [1 mark]But the program produces an incorrect output or result
Answer
  • [1 mark]The comparison operator was<=instead of>=
  • [1 mark]This would only allow users aged 18 or under to vote — the opposite of what was intended
  • [1 mark]The fix is to change the operator from<=to>=
Answer
  • [1 mark]The condition only rejects negative values
  • [1 mark]It does not reject 0, even though 0 is not a positive value
  • [1 mark]The fix is to change< 0to<= 0
Answer
  • [1 mark]Incorrect use of operators (<instead of>, or<=instead of>=)
  • [1 mark]Using the wrong variable in a calculation or comparison
  • [1 mark]Wrong divisor in an average calculation / looping one extra time / usingANDinstead ofOR

4. Runtime Errors in Pseudocode

Aruntime erroris where an error causes a program tocrashwhile it is running.

Examples of runtime errors
  • Dividing a number by 0
  • An index out of the range of an array
  • Unable to read or write to a drive
  • Entering text where a number is expected

4.1 Division by Zero — Runtime Error

The pseudocode below asks the user for two numbers and divides them. If the user enters 0 for the second number, the program crashes.

DECLARE number1 : INTEGER
DECLARE number2 : INTEGER
DECLARE result : REAL
OUTPUT "Enter the first number: "
INPUT number1
OUTPUT "Enter the second number: "
INPUT number2
result ← number1 / number2
OUTPUT "The result is ", result

Runtime Error: Division by zero

Ifnumber2is 0, the lineresult ← number1 / number2attempts to divide by zero, which is mathematically undefined.

Impact:Causes the program to crash.

Fix:Add a check before the division to make surenumber2is not 0.

Corrected pseudocode (runtime fixed):

DECLARE number1 : INTEGER
DECLARE number2 : INTEGER
DECLARE result : REAL
OUTPUT "Enter the first number: "
INPUT number1
OUTPUT "Enter the second number: "
INPUT number2
IF number2 = 0 THEN
OUTPUT "Error: cannot divide by zero."
ELSE
result ← number1 / number2
OUTPUT "The result is ", result
ENDIF

4.2 Array Index Out of Range — Runtime Error

This pseudocode declares an array of 5 elements but tries to access element 10, which does not exist.

DECLARE numbers : ARRAY[1:5] OF INTEGER
DECLARE index : INTEGER
FOR index ← 1 TO 5
numbers[index] ← index * 10
NEXT index
OUTPUT numbers[10]

Runtime Error: Index out of range

The arraynumbershas indices 1 to 5. Accessingnumbers[10]goes beyond the array's bounds.

Impact:Causes the program to crash.

Fix:Change the output line to a valid index, e.g.OUTPUT numbers[5].

4.3 Type Mismatch — Runtime Error

This pseudocode asks the user for a number but if the user types in text, the program crashes when trying to do arithmetic.

DECLARE age : INTEGER
DECLARE ageNextYear : INTEGER
OUTPUT "Enter your age: "
INPUT age
ageNextYear ← age + 1
OUTPUT "Next year you will be ", ageNextYear

Runtime Error: Type mismatch

If the user enters text such as "abc" for the age, the program cannot add 1 to it because "abc" is not a number.

Impact:Causes the program to crash.

Fix:Validate the input before performing the calculation — keep asking until a whole number is entered.

Corrected pseudocode (runtime fixed):

DECLARE age : INTEGER
DECLARE ageNextYear : INTEGER
REPEAT
OUTPUT "Enter your age: "
INPUT age
IF age < 0 OR age > 120 THEN
OUTPUT "Please enter a valid age."
ENDIF
UNTIL age >= 0 AND age <= 120
ageNextYear ← age + 1
OUTPUT "Next year you will be ", ageNextYear

Activity 4: Find the Runtime Error

The pseudocode below calculates the average of the values in an array of 5 numbers. It contains a runtime error.

DECLARE values : ARRAY[0:4] OF INTEGER
DECLARE index : INTEGER
DECLARE total : INTEGER
DECLARE average : REAL
values ← [10, 20, 30, 40, 50]
total ← 0
FOR index ← 1 TO 5
total ← total + values[index]
NEXT index
average ← total / 5
OUTPUT "Average: ", average
  1. Identify the runtime error.
  2. Explain the effect it has on the program.
  3. Suggest a fix.
Solution:
  1. Error:The arrayvaluesis declared with indices0:4(5 elements). However, the loop runs from1 TO 5, sovalues[5]is out of range.
  2. Effect:The program crashes when it tries to accessvalues[5].
  3. Fix:Change the loop toFOR index ← 0 TO 4so it matches the array bounds.

Corrected code:

FOR index ← 0 TO 4
total ← total + values[index]
NEXT index

Check Your Understanding: Runtime Errors in Pseudocode

Answer
  • [1 mark]An error that occurs while the program is running
  • [1 mark]It causes the program to crash
Answer
  • [1 mark]Dividing a number by 0
  • [1 mark]An index out of the range of an array
  • [1 mark]Unable to read or write to a drive / entering text where a number is expected
Answer
  • [1 mark]If the user enters 0 for the second number, the program tries to divide by zero
  • [1 mark]Division by zero is undefined and causes the program to crash
  • [1 mark]Fix: add anIF number2 = 0check before dividing and display an error message instead
Answer
  • [1 mark]"Causes the program to crash"

Key Takeaways

  • There arethree main categories of errors: syntax, logic and runtime.
  • Asyntax errorbreaks the grammatical rules of pseudocode andprevents the program from running.
  • Common syntax errors in pseudocode: missingTHEN, missingENDIF, missingNEXT, missing closing quote, misspelled keywords.
  • Alogic errorallows the program to run butproduces incorrect output.
  • Common logic errors in pseudocode: using<=instead of>=,ANDinstead ofOR, wrong divisor, wrong variable used.
  • Aruntime errorcauses the program to crash.
  • Common runtime errors in pseudocode: dividing by zero, array index out of range, type mismatch.
  • In exams, use the exact phrases:"prevents the program from running"(syntax),"produces incorrect output"(logic),"causes the program to crash"(runtime).
  • Trace tablesare an excellent way to find logic errors because they show the value of every variable at every step.
  • Alwaysname the error type,locate it, andexplain the impact— vague answers do not earn marks.

Question Bank

Answer
  • [2 marks]Syntax error— breaks the grammatical rules of pseudocode; prevents the program from running
  • [2 marks]Logic error— incorrect code that allows the program to run but produces incorrect output
  • [2 marks]Runtime error— an error that causes the program to crash while running
Answer
  • [1 mark]Syntax error:THENis missing after the IF condition
  • [1 mark]Logic error:the operator is<=, which allows 18 or under — should be>=
  • [3 marks]Corrected code:
CONSTANT VotingAge ← 18
OUTPUT "How old are you?"
INPUT YourAge
IF (YourAge >= VotingAge) THEN
OUTPUT "You can vote"
ENDIF
Answer
  • [2 marks]Asyntax errorbreaks the grammatical rules of pseudocode andprevents the program from running
  • [2 marks]Alogic errorallows the program to run without any error message, but itproduces incorrect output— the programmer must use a trace table or dry run to find it
Answer
  • [1 mark]Dividing a number by 0
  • [1 mark]An index out of the range of an array
  • [1 mark]Entering text where a number is expected (type mismatch)
Answer
  • [1 mark]The average is calculated by dividing by 4 instead of 5
  • [1 mark]This produces an incorrect output — the average will be too high
  • [1 mark]Corrected line:average ← total / 5
Answer
  • [1 mark]A trace table records the value of every variable at every step of execution
  • [1 mark]This makes it easy to spot the exact moment where a variable takes an unexpected value
  • [1 mark]By comparing the actual output to the expected output, the programmer can locate and fix the error
Answer
DECLARE Age : INTEGER
OUTPUT "Enter your age: "
INPUT Age
IF Age < 13 THEN
OUTPUT "Child"
ELSE IF Age <= 19 THEN
OUTPUT "Teenager"
ELSE
OUTPUT "Adult"
ENDIF

Marking:DECLARE (1), INPUT (1), correct IF for Child (1), correct ELSE IF for Teenager (1), correct ELSE for Adult (1), ENDIF (1).

Answer
  • [1 mark]The condition only rejectsnegativevalues — it does not rejectzero
  • [1 mark]Zero is not a positive value, so it should also be rejected
  • [1 mark]Fix: change the condition toIF length <= 0 OR width <= 0 THEN