You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers the three types of programming errors — syntax errors, logic errors, and runtime errors — as required by OCR J277 Section 2.4. Being able to identify, explain, and fix these errors is essential for the OCR GCSE Computer Science exam.
| Error Type | When Detected | Stops the Program? | Example |
|---|---|---|---|
| Syntax error | Before the program runs (at compile/parse time) | Yes — program will not start | Missing bracket, misspelled keyword |
| Logic error | During execution (but program does not crash) | No — runs but gives wrong results | Using + instead of - |
| Runtime error | During execution | Yes — program crashes | Division by zero, file not found |
flowchart TD
P[Programming error] --> S[Syntax error]
P --> L[Logic error]
P --> R[Runtime error]
S --> S1["Detected before run<br/>by compiler/interpreter"]
S --> S2["Examples: missing bracket,<br/>missing colon, typo in keyword"]
L --> L1["Detected by comparing<br/>actual vs expected output"]
L --> L2["Examples: wrong operator,<br/>off-by-one, wrong variable"]
R --> R1["Detected during execution<br/>— program crashes"]
R --> R2["Examples: divide by zero,<br/>index out of range, type mismatch"]
A syntax error occurs when the code breaks the rules (grammar) of the programming language. The interpreter or compiler cannot understand the instruction and refuses to run the program.
| Error | Example (Python) | Fix |
|---|---|---|
| Missing colon | if x == 5 | if x == 5: |
| Missing bracket | print("hello" | print("hello") |
| Misspelled keyword | whle x > 0: | while x > 0: |
| Missing quotation mark | print("hello) | print("hello") |
| Wrong indentation | if x == 5:\nprint("yes") | Indent the print line |
| Using = instead of == | if x = 5: | if x == 5: |
OCR Pseudocode example with syntax errors:
// INCORRECT — missing 'then' and 'endif'
if mark >= 50
print("Pass")
// CORRECT
if mark >= 50 then
print("Pass")
endif
Syntax errors are the easiest to fix because:
OCR Exam Tip: When asked to identify a syntax error in code, look for: missing brackets, missing colons (Python), misspelled keywords, missing
then/endif(pseudocode), and incorrect indentation. Always state what the error is AND how to fix it for full marks.
A logic error occurs when the program runs without crashing but produces incorrect results. The code is syntactically valid but the algorithm or logic is wrong.
| Error | Incorrect Code | Correct Code |
|---|---|---|
| Wrong operator | average = total * count | average = total / count |
| Wrong comparison | if age > 18 (excludes 18) | if age >= 18 (includes 18) |
| Off-by-one | for i = 0 to 10 (11 items) | for i = 0 to 9 (10 items) |
| Wrong variable | total = total + price (should be quantity * price) | total = total + quantity * price |
| Infinite loop | while x > 0\n print(x) (x never decreases) | Add x = x - 1 inside the loop |
| Condition always true | while true (no exit condition) | Add a condition that can become false |
Example — a logic error that calculates the wrong average:
scores = [80, 70, 90, 60, 50]
total = 0
for i = 0 to scores.length - 1
total = total + scores[i]
next i
average = total / 4 // LOGIC ERROR: should divide by 5 (scores.length)
print("Average: " + str(average))
The program runs fine but gives 70 instead of the correct answer 70... actually both give 70 in this case. Let us use a clearer example:
scores = [80, 70, 90, 60]
total = 0
for i = 0 to scores.length - 1
total = total + scores[i]
next i
average = total / 3 // LOGIC ERROR: should divide by 4
print("Average: " + str(average))
// Outputs 100 instead of correct answer 75
Logic errors are the hardest to find because:
OCR Exam Tip: Logic errors are the most commonly asked about in the exam. A typical question shows code with a logic error and asks you to identify it, explain what it does wrong, and suggest a fix. Always trace through the code step by step to find the error.
A runtime error occurs while the program is running, causing it to crash or terminate unexpectedly. The code is syntactically correct but encounters a situation it cannot handle.
| Error | Cause | Example |
|---|---|---|
| Division by zero | Dividing a number by zero | result = 10 / 0 |
| File not found | Trying to open a file that does not exist | openRead("missing.txt") |
| Index out of range | Accessing an array index that does not exist | list[10] when list has 5 items |
| Type mismatch | Performing an operation on incompatible types | int("hello") |
| Stack overflow | Infinite recursion | A function that calls itself forever |
| Out of memory | Program uses more memory than available | Creating an infinitely growing list |
Python example:
numbers = [10, 20, 30]
print(numbers[5]) # Runtime error: IndexError — list index out of range
| Feature | Syntax Error | Logic Error | Runtime Error |
|---|---|---|---|
| When detected | Before running | During or after running | During running |
| Program runs? | No | Yes | Yes, but then crashes |
| Error message? | Yes — with line number | No | Yes — when it crashes |
| Difficulty to find | Easy | Hard | Medium |
| Difficulty to fix | Easy | Can be hard | Medium |
| Detection method | Compiler/interpreter | Testing and trace tables | Testing |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.