What are syntax errors and logic errors, and how do you tell them apart?
The two main types of programming error: syntax errors and logic errors, what causes each, how they are found, and how they differ.
An OCR J277 2.3.2 answer on the two main types of programming error: syntax errors (breaking the rules of the language) and logic errors (the program runs but gives the wrong result), what causes each, and how they are found and corrected.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
What this dot point is asking
OCR wants you to know the two main types of programming error, syntax errors and logic errors, what causes each, how they are found, and how they differ. The key distinction is that a syntax error stops the program running, while a logic error lets it run but produces the wrong result. This is examined in Paper 2, usually as "explain the difference" or "state which type".
Syntax errors
Logic errors
Telling them apart
The fastest test is what happens when you run the program. If it will not run and the translator reports an error, it is a syntax error. If it runs but gives the wrong result with no error message, it is a logic error. This is why logic errors are more dangerous: the program appears to work, so the bug can go unnoticed until it produces a wrong answer in use.
Try this
Q1. State what a syntax error is. [1 mark]
- Cue. An error that breaks the rules (grammar) of the programming language, so the program will not run.
Q2. A program runs but always outputs a value that is too small. Which type of error is this? [1 mark]
- Cue. A logic error (it runs but gives the wrong result).
Q3. State why logic errors are harder to find than syntax errors. [1 mark]
- Cue. No error message is produced and the program does not stop, so you must work out that the output is wrong and track down the faulty logic yourself.
Exam-style practice questions
Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
OCR 20214 marksExplain the difference between a syntax error and a logic error, giving an example of each.Show worked answer →
Syntax error (up to 2): an error that breaks the grammar or rules of the programming language, so the program will not run (or will not translate). Example: a missing bracket, a misspelled keyword such as pirnt instead of print, or a missing endif.
Logic error (up to 2): an error where the program runs without crashing but produces the wrong result, because the logic of the algorithm is faulty. Example: using + instead of -, the wrong condition such as > instead of >=, or total = i instead of total = total + i.
Markers reward the breaks-the-rules-so-it-will-not-run distinction for syntax, the runs-but-wrong-result distinction for logic, and a matching example of each. A common error is giving a logic-error example for a syntax error.
OCR 20223 marksA program runs without any error messages but always outputs the wrong total. State which type of error this is, explain how you could find it, and state why this type is harder to find than the other.Show worked answer →
Type (1): a logic error, because the program runs but gives the wrong result.
How to find it (1): trace the program by hand using a trace table, or add print statements (or use the debugger) to watch the variables and find where the value goes wrong.
Why harder to find (1): a logic error does not produce an error message and does not stop the program, so the computer cannot point to it; you must work out that the output is wrong and track down the faulty logic yourself, unlike a syntax error which the translator flags.
Markers reward identifying it as a logic error, a sensible method (tracing or debugging), and the reason that no error message is generated.
Related dot points
- The purpose of testing, the difference between iterative and terminal (final) testing, and the types of test data (normal, boundary and erroneous or invalid), with how to choose suitable test data.
An OCR J277 2.3.2 answer on testing: the purpose of testing, iterative versus terminal (final) testing, and the three types of test data (normal, boundary and erroneous or invalid), with how to choose suitable test data and build a test plan.
- Defensive design: anticipating misuse, input validation and sanitisation, authentication, and writing maintainable programs through comments, indentation and sensible naming.
An OCR J277 2.3.1 answer on defensive design: anticipating misuse, validating and sanitising input, authentication, and writing maintainable programs with comments, indentation and sensible variable names.
- Using trace tables to determine the output of an algorithm and to follow how the values of variables change, and determining the purpose of a simple algorithm.
An OCR J277 2.1.2 answer on using trace tables to follow an algorithm step by step, record how variable values change, find the output, and determine the purpose of a simple algorithm.
- The purpose of translators, the characteristics of a compiler and an interpreter and how they differ, and the role of an assembler in translating assembly language.
An OCR J277 2.5.2 answer on translators: why source code must be translated, the characteristics of a compiler and an interpreter and how they differ, and the role of an assembler in translating assembly language to machine code.
- Producing algorithms using pseudocode and flowcharts to solve a problem, identifying the inputs, processes and outputs, and interpreting, correcting and refining algorithms others have written.
An OCR J277 2.1.2 answer on designing algorithms with pseudocode and flowcharts: identifying inputs, processes and outputs, the OCR Exam Reference Language, the standard flowchart symbols, and interpreting, correcting and refining algorithms.
Sources & how we know this
- OCR GCSE (9-1) Computer Science (J277) specification — OCR (2020)