Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Testing

Identify syntax and logic errors

All topicsPractise exam questions
Knowledge organiser

Testing

2.3.2c

Syntax errors as errors which break the grammatical rules of the programming language and stop it from being run/translated; Logic errors as errors which produce unexpected output;

What you need to know

Syntax errors break the grammatical rules of a programming language and stop the program from running or being translated. Logic errors allow the program to run but produce incorrect or unexpected results. Identifying both types of error is a key skill that requires careful testing and code review.

Key points

  • Definition:Syntax error: breaks the rules/grammar of the programming language and prevents the program from running/executing/compiling.
  • Definition:Logic error: an error where the program runs but produces unexpected/incorrect output.
  • Exam Tip:A definition requires the general rule. 'A misspelling of a keyword' is an EXAMPLE, not a definition. The definition is 'breaks the rules/grammar of the language'.
  • Exam Tip:'Does not work' or 'stop working' are too vague — say 'prevents the program from running' (syntax) or 'produces incorrect output' (logic).
  • Common syntax errors: misspelled keywords (printt), missing brackets, missing colons, missing quotation marks, using =< instead of <=, print(Not in demand) — missing string delimiters.
  • Syntax errors are caught by the IDE or translator and are relatively easy to fix.
  • Logic errors are harder to spot because the program does not crash — it runs but gives wrong results.
  • Exam Example:total = num1 + num1 instead of total = num1 + num2 — using wrong variable is a logic error (line 03).
  • Exam Example:if total >= 10 then — only checks one boundary; should be if total >= 10 AND total <= 20 then — incomplete condition is a logic error (line 04).
  • Exam Example:for scoreCount = 1 to scores.length - 1 misses index 0; should start at 0. And scores[scoreCount] = total + total should be total = total + scores[scoreCount].
  • Exam Example:if stocklevel >= 5 or =< 25 has THREE errors: or→AND, =<→<=, missing variable name in second comparison. Also print(Not in demand) needs quotation marks. Also output messages are swapped.
  • Testing with different types of data (normal, boundary, erroneous) helps identify logic errors.

Code examples

Identifying errors
python
# SYNTAX ERROR examples:
# print("Hello"    <- missing closing bracket
# if x == 5         <- missing colon
# pritn("Hi")      <- misspelled keyword
# print(Not in demand) <- missing string quotes

# LOGIC ERROR examples:
# total = 0 inside a loop (resets each iteration)
# print("empty") prints the word, not the variable
# for i in range(1, 9) misses index 0 of an array
for i in range(9):     # Correct: starts from 0
    total = total + room[i]