Faults & Errors (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Error types

What are the common types of error?

  • Designing algorithms is a skill that must be developed and when designing algorithms, mistakes and issues will occur

  • Trace tables can also help to find any kind of error in a program or algorithm

  • There are three main categories of errors that when designing algorithms a programmer must be able to identify & fix, they are:

    • Syntax errors

    • Logic errors

    • Runtime errors

Syntax errors

What is a syntax error?

  • A syntax error is an error that breaks the grammatical rules of a programming language and stops it from running

  • Examples of syntax errors are:

    • Typos and spelling errors 

    • Missing or extra brackets or quotes

    • Misplaced or missing semicolons

    • Invalid variable or function names

    • Incorrect use of operators

    • Incorrectly nested loops & blocks of code

Pseudocode - with syntax errors

FUNCTION generate_username(first_name STRING, last_name STRING) RETURNS STRING
    DECLARE username : STRING
    username = SUBSTRING(first_name, 1, 1) + SUBSTRING(last_name, 1, 1) + SUBSTRING(last_name, 1, 3)
    RETURN username
ENDFUNCTION

PROCEDURE main()
    DECLARE first_name : STRING
    DECLARE last_name : STRING
    DECLARE username : STRING

    OUTPUT "Enter your first name: "
    INPUT first_name

    OUTPUT "Enter your last name: "
    INPUT last_name

    username ← generate_username[first_name, last_name]

    OUTPUT "Here's a suggested username:" + username
ENDPROCEDURE

// Main program
CALL Main

Pseudocode - without syntax errors

FUNCTION generate_username(first_name : STRING, last_name : STRING) RETURNS STRING
    DECLARE username : STRING
    username ← SUBSTRING(first_name, 1, 1) + SUBSTRING(last_name, 1, 1) + SUBSTRING(last_name, 1, 3)
    RETURN username
ENDFUNCTION

PROCEDURE main()
    DECLARE first_name : STRING
    DECLARE last_name : STRING
    DECLARE username : STRING

    OUTPUT "Enter your first name: "
    INPUT first_name

    OUTPUT "Enter your last name: "
    INPUT last_name

    username ← generate_username(first_name, last_name)

    OUTPUT "Here's a suggested username: ", username
ENDPROCEDURE

// Main program
CALL main()

Syntax errors

  • FUNCTION generate_username(first_name STRING...)

    • Missing colon : between parameter name and type

    • first_name : STRING and last_name : STRING

  • username = SUBSTRING(...)

    • Uses = instead of correct assignment operator ←

    • Should be username ← ...

  • username ← generate_username[first_name, last_name]

    • Incorrect use of square brackets [] instead of parentheses () for function call

    • Should be generate_username(first_name, last_name)

  • CALL Main

    • Incorrect capitalisation and missing parentheses — procedure name is case-sensitive

    • Should be CALL main()

Logic errors

What is a logic error?

  • A logic error is 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 use 'Trace Tables'

  • Examples of logic errors are:

    • Incorrect use of operators  (< and >)

    • Logical operator confusion (AND for OR)

    • Looping one extra time

    • Indexing arrays incorrectly (arrays indexing starts from 0)

    • Using variables before they are assigned

    • Infinite loops

Pseudocode

FUNCTION calculate_area(length : REAL, width : REAL) RETURNS REAL
    // Checks for valid dimensions and calculates area
    IF length > 0 OR width > 0 THEN
        DECLARE area : REAL
        area ← width - length
        RETURN area
    ELSE
        OUTPUT "Length and width must be positive values."
    ENDIF
ENDFUNCTION

PROCEDURE main()
    DECLARE length : REAL
    DECLARE width : REAL
    DECLARE area : REAL

    OUTPUT "Enter the length of the rectangle: "
    INPUT length

    OUTPUT "Enter the width of the rectangle: "
    INPUT width

    area ← calculate_area(length, width)

    OUTPUT "The area of the rectangle is approximately ", area, " square units."
ENDPROCEDURE

// Main program
CALL main()

Logic errors

Line

Error

Correction

IF length > 0 OR width > 0 THEN

The check allows one value to be negative, which is not valid

Should be IF length > 0 AND width > 0 THEN

area ← width - length

Incorrect calculation – subtracts instead of multiplying to find the area

Should be area ← length * width

Runtime errors

What is a runtime error?

  • A runtime error is where an error causes a program to crash

  • Examples of runtime errors are:

    • Dividing a number by 0

    • Index out of the range of an array

    • Unable to read or write a drive

Pseudocode

FUNCTION calculate_area(length : REAL, width : REAL) RETURNS REAL
    // Calculates area of a rectangle after input validation
    IF length < 0 OR width < 0 THEN
        OUTPUT "Length and width must be positive values."
    ENDIF

    DECLARE area : REAL
    area ← length * width
    RETURN area
ENDFUNCTION

PROCEDURE main()
    DECLARE length : REAL
    DECLARE width : REAL
    DECLARE area : REAL

    OUTPUT "Enter the length of the rectangle: "
    INPUT length

    OUTPUT "Enter the width of the rectangle: "
    INPUT width

    area ← calculate_area(length, width)

    OUTPUT "The area of the rectangle is approximately ", area, " square units."
ENDPROCEDURE

// Main program
CALL main()

Runtime error

Line

Error

Correction

area ← calculate_area(length)

Only one parameter is passed instead of two – will crash at runtime

Should be area ← calculate_area(length, width)

Worked Example

Each pseudocode statement in the following table may contain an error due to the incorrect use of the function or operator.

Describe the error in each case, or write ‘NO ERROR’ if the statement contains no error.

You can assume that none of the variables referenced are of an incorrect type.[5]

Statement

Error

Result ← 2 & 4

SubString ← MID("pseudocode", 4, 1)

IF x = 3 OR 4 THEN

Result ← Status AND INT(x/2)

Message ← "Done" + LENGTH(MyString)

Answer

Statement

Error

Result ← 2 & 4

Should be arithmetic

operator (not &) OR 2 and 4

should be CHAR / STRING

SubString ← MID("pseudocode", 4, 1)

NO ERROR

IF x = 3 OR 4 THEN

Not Boolean values /

incorrect operator OR

Condition incorrect

Result ← Status AND INT(x/2)

INT(X/2) doesn't evaluate

to a Boolean value /

incorrect operator

Message ← "Done" + LENGTH(MyString)

Can’t add string to number /

"Done" is not a number

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.