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):
- A missingTHENkeyword after anIFstatement
- Using<instead of>in a comparison
- Dividing a number by zero
- Accessing an array index that is out of range
- A missing closing quotation mark on a string
- A loop that runs one too many times
Solution:
- Syntax error— missing keyword prevents the program from running.
- Logic error— the program runs but produces incorrect output.
- Runtime error— dividing by zero causes the program to crash.
- Runtime error— an out-of-range index causes the program to crash.
- Syntax error— a missing quote prevents the program from running.
- Logic error— the program runs but produces incorrect output.
Check Your Understanding: Types of Errors
1. Name the three main categories of errors in programming. [3 marks]
Answer
- [1 mark]Syntax errors
- [1 mark]Logic errors
- [1 mark]Runtime errors
2. Complete the table showing the correct exam phrase for each error type. [3 marks]
Answer
| Error type | Exam phrase |
|---|---|
| Syntax error | "prevents the program from running" |
| Logic error | "produces incorrect output" |
| Runtime error | "causes the program to crash" |
3. Why is a logic error often harder to find than a syntax error? [3 marks]
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
4. Describe a runtime error and give two examples. [4 marks]
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:
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):
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.
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:
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.
Solution:
- Error:TheFORloop is missingNEXT Countto close it.
Fix:AddNEXT Countafter the OUTPUT line. - 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:
Check Your Understanding: Syntax Errors in Pseudocode
1. What is a syntax error in pseudocode? [2 marks]
Answer
- [1 mark]A syntax error breaks the grammatical rules of pseudocode
- [1 mark]It prevents the program from running
2. Identify the two syntax errors in the voting age program. [2 marks]
Answer
- [1 mark]MissingTHENafter theIFcondition
- [1 mark]Missing closing double quote on"You are not old enough to vote
3. Explain what happens when pseudocode contains a syntax error. [2 marks]
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
4. Give three examples of syntax errors in pseudocode. [3 marks]
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:
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):
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.
| Test | Test data | Expected outcome | Actual outcome | Change needed? |
|---|---|---|---|---|
| 1 | length = 5 width = 5 | "The area is 25" | "The area is 25" | N |
| 2 | length = 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):
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.
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):
Activity 3: Find the Logic Error
The pseudocode below is supposed to output the highest of three numbers. It contains a logic error.
- Identify the logic error.
- Explain the effect it has on the output.
- Suggest a fix.
Solution:
- Error:The comparison operators are<instead of>. The program finds thesmallestnumber, not the highest.
- Effect:The program runs but produces incorrect output — it outputs the lowest number instead of the highest.
- Fix:Change both<to>:IF b > highest THENhighest ← bENDIFIF c > highest THENhighest ← cENDIF
Check Your Understanding: Logic Errors in Pseudocode
1. What is a logic error? [2 marks]
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
2. In the voting age program, what logic error was found and how was it fixed? [3 marks]
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>=
3. In the rectangle area program, why isIF length < 0 OR width < 0a logic error? [3 marks]
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
4. Give three examples of logic errors in pseudocode. [3 marks]
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.
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):
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.
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.
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):
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.
- Identify the runtime error.
- Explain the effect it has on the program.
- Suggest a fix.
Solution:
- Error:The arrayvaluesis declared with indices0:4(5 elements). However, the loop runs from1 TO 5, sovalues[5]is out of range.
- Effect:The program crashes when it tries to accessvalues[5].
- Fix:Change the loop toFOR index ← 0 TO 4so it matches the array bounds.
Corrected code:
Check Your Understanding: Runtime Errors in Pseudocode
1. What is a runtime error? [2 marks]
Answer
- [1 mark]An error that occurs while the program is running
- [1 mark]It causes the program to crash
2. Give three examples of runtime errors. [3 marks]
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
3. In the division program, what caused the runtime error and how was it fixed? [3 marks]
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
4. What phrase should you use in an exam to describe the impact of a runtime error? [1 mark]
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
1. Name and describe the three main categories of programming errors. [6 marks]
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
2. The following pseudocode is intended to check if a user is 18 or over. Identify the errors and correct them. [5 marks]
CONSTANT VotingAge ← 18
OUTPUT "How old are you?"
INPUT YourAge
IF (YourAge <= VotingAge)
OUTPUT "You can vote"
ENDIF
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:
3. Explain the difference between a syntax error and a logic error in pseudocode. [4 marks]
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
4. Give three examples of runtime errors in pseudocode. [3 marks]
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)
5. A programmer writes the following pseudocode to find the average of 5 numbers. Identify the logic error and correct it. [3 marks]
total ← 0
FOR count ← 1 TO 5
total ← total + count
NEXT count
average ← total / 4
OUTPUT average
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
6. Explain how a trace table can help identify logic errors in an algorithm. [3 marks]
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
7. Write CIE pseudocode for a program that asks the user for their age and outputs "Child" if under 13, "Teenager" if 13–19, and "Adult" if 20 or over. [6 marks]
Answer
Marking:DECLARE (1), INPUT (1), correct IF for Child (1), correct ELSE IF for Teenager (1), correct ELSE for Adult (1), ENDIF (1).
8. A program contains the line:IF length < 0 OR width < 0 THEN. Explain why this is a logic error and suggest a fix. [3 marks]
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