Syntax, logic and runtime errors

GCSE Computer Science (Edexcel) · Edexcel 1CP2 · 1.2.5 · Computational thinking › Algorithms

69 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (Edexcel), marked as you tap.

Revision notes

Programs can contain three types of error: syntax, logic and runtime errors. You need to recognise each type, and to find and correct logic errors in algorithms. Questions ask you to identify the type of an error, explain the difference between types, and fix the lines that cause a wrong result.

Key facts

  • Syntax error: breaks the rules of the language; the program will not run
  • Logic error: the program runs but gives the wrong result
  • Runtime error: happens while the program is running and makes it crash
  • Syntax examples: missing colon, misspelt keyword, unmatched brackets, wrong indentation
  • Logic examples: wrong operator, wrong loop range, missing brackets in a formula
  • Runtime examples: division by zero, index out of range, invalid type conversion
  • Find logic errors with test data (expected v actual results) and trace tables

Notes

Syntax errors

  • A syntax error breaks the rules (grammar) of the programming language, so the code cannot be translated and the program will not run.
  • Examples: a misspelt keyword (pritn), a missing colon after if or while, brackets or quotation marks that do not match, missing or unexpected indentation in Python.
  • The translator reports syntax errors, usually with a line number, so they are the easiest type to find.

Logic errors

  • A logic error lets the program run, but it gives the wrong result or does not do what was intended.
  • There is no error message, so you find logic errors by testing with data whose correct result you already know, and by tracing.
  • Common causes: the wrong relational operator (> instead of >=), and instead of or, a loop that runs one time too many or too few, missing brackets in a formula, a line indented at the wrong level (so it runs inside or outside a loop by mistake), a total that is reset inside a loop.
  • Example: average = a + b / 2 runs, but only b is divided by 2. The correction is average = (a + b) / 2.

Runtime errors

diagram
  • A runtime error happens while the program is running. The syntax is correct, but the program is asked to do something impossible, so it crashes (stops).
  • Examples: dividing by zero, using an index outside the array (scores[4] when there are only 4 items), converting text such as "abc" to an integer, opening a file that does not exist.
  • Source codeTranslateRunOutputSyntax error:will not translateRuntime error:crashes while runningLogic error:runs, but theresult is wrong
    Syntax errors are found before the program runs; runtime and logic errors only appear when it runs
  • Many runtime errors can be prevented by checking first: only divide if the count is not 0, check an index is less than the length of the list, and validate input before converting it. Grade 8+ only

How to answer each type of question

Identify the type of error

1 mark eachGrade 5
  1. Does the code break the rules of the language? Syntax error.
  2. Does it run but give the wrong answer? Logic error.
  3. Does it crash part-way through running? Runtime error.

Example. State the type of error in each of these.
(a) print("Total" total)
(b) area = length + width in a program that should calculate the area of a rectangle
(c) average = total / count when count is 0

Show the model answerHide the model answer
(a) Syntax error (1): there is no comma between the two items
(b) Logic error (1): it should multiply, not add
(c) Runtime error (1): division by zero

Explain the difference between two types of error

2 marksGrade 5
  1. Describe the first type: what happens and when.
  2. Describe the second type in the same way, so that the difference is clear.

Example. Explain the difference between a syntax error and a logic error.

Show the model answerHide the model answer
A syntax error breaks the rules of the programming language, so the program will not run (1). A program with a logic error does run, but it produces the wrong result (1).

Find and correct logic errors

2 to 4 marksGrade 7
  1. Work out the result the program should give for the data.
  2. Trace the program to see what it actually does and where it goes wrong.
  3. For each error, give the line number, say what is wrong and write the corrected line.

Example. This program should count how many marks are 40 or more. For this list it should output 3.

01 marks = [62, 40, 35, 18, 55]
02 passes = 0
03 for i in range(1, len(marks)):
04     if marks[i] > 40:
05         passes = passes + 1
06 print(passes)
There are two logic errors. Give the line number of each error and the corrected code.

Show the model answerHide the model answer
Line 03 (1): the loop starts at index 1, so the first mark (62) is never checked. Correct it to for i in range(len(marks)):
(1).
Line 04 (1): > does not count a mark of exactly 40. Correct it to if marks[i] >= 40:
(1).
As written, the program outputs 1.

Don’t lose marks

  • Calling a crash (such as division by zero) a logic error. It is a runtime error.
  • Saying a logic error stops the program. It does not: the program runs but gives the wrong result.
  • Describing a fix vaguely ('change the loop'). Write the corrected line of code.
  • Changing a line that was already correct, or making a fix that introduces a new error.

More tips

Memory tricks

  • Syntax: won't run. Logic: runs, but wrong. Runtime: runs, then crashes.
  • A syntax error is like a spelling or grammar mistake; a logic error is like following a recipe with the wrong amount of sugar.
  • Check the usual suspects for logic errors: < v <=, and v or, where a loop starts and ends, brackets, and where a total is set to 0.

Exam technique

  • For each error, give three things: the line number, what is wrong, and the corrected code.
  • Use the expected output given in the question: trace the code, then check that your corrected version gives that output.
  • If a question says there are two errors, look for two different lines.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    Name the three types of errorSyntax, logic and runtime errors.
  2. Grade 4
    Describe each type of errorSyntax breaks the language rules; logic gives wrong results; runtime crashes while running.
  3. Grade 5
    Identify the type of a given errorDecide whether it stops the program running, gives a wrong result or makes it crash.
  4. Grade 6
    Find a logic error using test dataCompare the expected result with the actual result, then trace to find the line.
  5. Grade 7
    Correct logic errors in an algorithmGive the line number, what is wrong and the corrected code.
  6. Grade 8
    Explain how to prevent runtime errorsE.g. check that a divisor is not zero, or that an index is in range, before using it.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

Look at this program.
01 x = 10
02 y = 0
03 print(x / y)
Give the type of error that happens when this program runs.
Runtime error
Name the type of error that does not cause an error message to be shown.
Logic error
Give two different things that could cause a runtime error in a program.
For example dividing by zero; using a list position that does not exist.

Sample questions

Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.

Question 1Easy4 marks
This program contains three syntax errors.
01 name = input("Enter your name: ")
02 if name = "Sam":
03     print("Welcome back, Sam")
04 else
05     print("Hello, " + name
(a) For each syntax error, give the line number and write the corrected line.
[3]
(b) Define the term 'syntax error'.
[1]
Show the answer and mark scheme
(a) Answer: Line 02: if name == "Sam":
Line 04: else:
Line 05: print("Hello, " + name)
  • line 02: if name == "Sam":
  • line 04: else:
  • line 05: print("Hello, " + name)
(b) Answer: A mistake in the rules (grammar) of the programming language, which stops the program from being translated and run.
  • an error that breaks the rules / grammar of the programming language (so the program cannot be translated / run)
Question 2Medium6 marks
This program should ask for five numbers and output their mean. It contains a syntax error, a logic error and a runtime error.
01 total = 0
02 for count in range(5)
03     number = int(input("Enter a number: "))
04     total = number
05 average = total / 5
06 print("Average: " + average)
(a) Identify the line that contains the syntax error and write the corrected line.
[2]
(b) Identify the line that contains the logic error and write the corrected line.
[2]
(c) Line 06 causes a runtime error. Give the reason for this error.
[1]
(d) Write a corrected version of line 06.
[1]
Show the answer and mark scheme
(a) Answer: Line 02: for count in range(5):
  • line 02
  • for count in range(5):
(b) Answer: Line 04: total = total + number
  • line 04
  • total = total + number
(c) Answer: A string cannot be joined to a real number with +.
  • a string and a number / float cannot be joined (concatenated) with +
(d) Answer: print("Average: " + str(average))
  • print("Average: " + str(average)) / print("Average:", average)
Question 3Hard8 marks
A shop gives a discount on orders. Orders of £100 or more get 20% off. Orders of £50 or more, but under £100, get 10% off. Other orders get no discount. This program should output the price to pay.
01 total = float(input("Order total: "))
02 if total >= 50:
03     discount = 0.1
04 elif total >= 100:
05     discount = 0.2
06 else:
07     discount = 0
08 pay = total * discount
09 print("Price to pay:", pay)
(a) The order total is £120. State the output of the program as it is written.
[1]
(b) The program contains two logic errors. Identify each error and explain how to correct it.
[4]
(c) After the errors have been corrected, the program is tested.
Give three order totals that together test every discount band, including a boundary value. For each, give the expected price to pay.
[3]
Show the answer and mark scheme
(a) Answer: Price to pay: 12.0
  • Price to pay: 12.0 (accept 12)
(b) Answer: The conditions are in the wrong order, so an order of £100 or more is caught by total >= 50: check total >= 100 first. Line 08 calculates the discount instead of the price: use pay = total * (1 - discount).
  • the conditions are in the wrong order: an order of £100 or more is caught by total >= 50 / the 20% branch can never run
  • check total >= 100 first (swap the two conditions and their discounts)
  • line 08 calculates the amount of discount rather than the price to pay
  • pay = total * (1 - discount) / pay = total - total * discount
(c) Answer: For example: £30 → £30.00; £50 → £45.00; £150 → £120.00
  • a total below £50 with price equal to the total
  • a total from £50 to £99.99 with price = total × 0.9
  • a total of £100 or more with price = total × 0.8

More in Algorithms

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.