Identify Errors in Algorithms (OCR GCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

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

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

    • Syntax errors

    • Logic 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

Examples

Syntax Errors

Corrected

age = input("Enter age)

favNum == input("Enter favourite number")

print age + favNum)

print (age x favNum)

age = input("Enter age") # Missing "

favNum = input("Enter favourite number")
# Only one equal sign

print (age + favNum) # Missing bracket

print (age * favNum) # Multiply symbol is *

num1 = imput("Enter the first number")

num2 = input(Enter the second number)

if num1 > num2 then

print(num1 + " is larger")

elseif num2 > num1 then

  Print(num2 + " is larger")

else

  print("The numbers are the same")

endif

num1 = input("Enter the first number") # Misspelt word 

num2 = input("Enter the second number") # Missing quotes

if num1 > num2 then

  print(num1 + " is larger") # Block not indented

elseif num2 > num1 then

  print(num2 + " is larger") # Lowercase p

else

  print("The numbers are the same")

endif

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

Example

  • An algorithm is written to take as input the number of miles travelled. The algorithm works out how much this will cost, with each mile costing £0.30 in petrol. If this is greater than £10.00 then it is reduced by 10%.

Logic errors

Corrected

miles = input("Enter the number of miles)

 cost = 0.3

 if cost = 10 then

cost = cost * 0.1

 endif

 print(cost)

miles = input("Enter the number of miles")

cost = miles * 0.3

if cost > 10 then

cost = cost * 0.9

endif

print(cost)

Commentary

  • The cost was set to 0.3 (30p) instead of correctly calculating the cost of the trip by applying the formula, miles * 0.3

  • The cost should only be reduced if the cost is greater than 10, in the original algorithm it only checked if the cost was equal to 10

  • To calculate a discount of 10%, either calculate what 10% is and subtract it from the original or multiply the full cost by 0.9. In the original algorithm it calculates what 10% is and sets the cost to equal it.

Worked Example

Nine players take part in a competition, their scores are stored in an array. The array is named scores and the index represents the player

Array scores

Index

0

1

2

3

4

5

6

7

8

Score

7

9

2

11

8

4

13

10

5


The following program counts the total score of all the players

for x = 1 to 8

total = 0

total = total + scores[x]

next x

print(total)

When tested, the program is found to contain two logic errors.

Describe how the program can be refined to remove these logic errors [2]

How to answer this question

  • A common logic error if an algorithm contains a loop is checking it loops the correct amount of times, how many times should this algorithm loop?

Answer

  • For loop changed to include 0

  • total = 0 moved to before loop starts

Guidance

  • Moving total outside the loop is not enough, it could be moved after the loop which would still be a logic error)

  • Corrected code accepted

total = 0

for x = 0 to 8

total = total + scores[x]

next x

print(total)

Last updated:

You've read 0 of your 10 free revision notes

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

Author: James Woodhouse

Expertise: Computer Science

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.