Faults & errors (Cambridge (CIE) AS 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 GenerateUsername(FirstName STRING, LastName : STRING) RETURNS STRING
   DECLARE Username : STRING
   Username = LEFT(FirstName, 1) & LEFT(LastName, 3)
   RETURN Username
ENDFUNCTION

PROCEDURE Main()
   DECLARE FirstName : STRING
   DECLARE LastName : STRING
   DECLARE Username : STRING

   OUTPUT "Enter your first name: "
   INPUT FirstName
   OUTPUT "Enter your last name: "
   INPUT LastName
   Username ← GenerateUsername[FirstName, LastName]
   OUTPUT "Here is a suggested username: ", Username
ENDPROCEDURE

// Main program
CALL Main

Pseudocode - without syntax errors

FUNCTION GenerateUsername(FirstName : STRING, LastName : STRING) RETURNS STRING
   DECLARE Username : STRING
   Username ← LEFT(FirstName, 1) & LEFT(LastName, 3)
   RETURN Username
ENDFUNCTION

PROCEDURE Main()
   DECLARE FirstName : STRING
   DECLARE LastName : STRING
   DECLARE Username : STRING

   OUTPUT "Enter your first name: "
   INPUT FirstName
   OUTPUT "Enter your last name: "
   INPUT LastName
   Username ← GenerateUsername(FirstName, LastName)
   OUTPUT "Here is a suggested username: ", Username
ENDPROCEDURE

// Main program
CALL Main()

Syntax errors

  • FUNCTION GenerateUsername(FirstName STRING, ...)

    • Missing colon between the parameter name and its data type

    • Should be FirstName : STRING

  • Username = LEFT(...)

    • Uses = instead of the assignment operator ←

    • Should be Username ← ...

  • Username ← GenerateUsername[FirstName, LastName]

    • Square brackets [] used instead of parentheses () for a function call

    • Should be GenerateUsername(FirstName, LastName)

  • CALL Main

    • Missing parentheses in the procedure call

    • 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

    • Using an array index that is one too high or one too low

Pseudocode

FUNCTION CalculateArea(Length : REAL, Width : REAL) RETURNS REAL
   DECLARE Area : REAL
   // Checks for valid dimensions and calculates area
   IF Length > 0 OR Width > 0 THEN
      Area ← Width - Length
   ELSE
      OUTPUT "Length and width must be positive values."
      Area ← 0
   ENDIF
   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 ← CalculateArea(Length, Width)
   OUTPUT "The area of the rectangle is ", 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 or zero, 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 run-time error is an error that occurs while the program is running and causes it to crash or stop responding ('freeze')

  • Examples of run-time errors are:

    • Dividing a number by 0

    • Using an array index outside the bounds of the array

    • Calling a function with invalid parameters

    • A conditional loop whose terminating condition is never satisfied, so the program stops responding

    • Being unable to read or write a file

Pseudocode

FUNCTION AverageMark(Total : INTEGER, NumberOfStudents : INTEGER) RETURNS REAL
   RETURN Total / NumberOfStudents
ENDFUNCTION

PROCEDURE Main()
   DECLARE Total : INTEGER
   DECLARE NumberOfStudents : INTEGER
   DECLARE Average : REAL

   OUTPUT "Enter the total of all the marks: "
   INPUT Total
   OUTPUT "Enter the number of students: "
   INPUT NumberOfStudents
   Average ← AverageMark(Total, NumberOfStudents)
   OUTPUT "The average mark is ", Average
ENDPROCEDURE

// Main program
CALL Main()

Replace the run-time error table row with:
<tr><td>`RETURN Total / NumberOfStudents`</td><td>If the user enters 0 for the number of students, the program tries to divide by zero and crashes while it is running</td><td>Check the value before dividing:<br>`IF NumberOfStudents > 0 THEN`<br>`   RETURN Total / NumberOfStudents`<br>`ELSE`<br>`   RETURN 0`<br>`ENDIF`</td></tr>

Runtime error

Line

Error

Correction

RETURN Total / NumberOfStudents

If the user enters 0 for the number of students, the program tries to divide by zero and crashes while it is running

Check the value before dividing:

IF NumberOfStudents > 0 THEN

RETURN Total / NumberOfStudents

ELSE

RETURN 0

ENDIF

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

Unlock more, it's free!

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

the (exam) results speak for themselves:

Build on this topic

Robert Hampton

Author: Robert Hampton

Expertise: Curriculum Expert

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: Portfolio 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.