You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
A common question type in OCR J277 Paper 2 presents you with a piece of code that contains errors and asks you to identify and fix them. This lesson covers the types of errors you need to know and strategies for spotting them.
There are three main types of programming errors:
| Error type | Description | Example |
|---|---|---|
| Syntax error | The code breaks the rules of the programming language | Missing endif, misspelled keyword, missing bracket |
| Logic error | The code runs but produces the wrong result | Using + instead of -, wrong comparison operator |
| Runtime error | The code crashes during execution | Division by zero, accessing an invalid array index |
The decision tree below helps you classify a bug quickly during an error-spotting question:
flowchart TD
A[Run or trace the code] --> B{Does the code run at all?}
B -->|No| C[Syntax error — missing keyword/bracket/spelling]
B -->|Yes| D{Does it crash partway?}
D -->|Yes| E[Runtime error — division by zero, array bounds, type mismatch]
D -->|No| F{Output matches expected?}
F -->|No| G[Logic error — wrong operator, off-by-one, bad initialisation]
F -->|Yes| H[No error detected]
Syntax errors are usually the easiest to spot. The code will not run at all if a syntax error is present.
| Error | Incorrect | Correct |
|---|---|---|
| Missing closing keyword | if x > 5 then print("big") | if x > 5 then print("big") endif |
| Misspelled keyword | wihle x > 0 | while x > 0 |
| Missing bracket | print("Hello" | print("Hello") |
| Assignment vs comparison | if x = 5 then (in some contexts) | if x == 5 then |
| Wrong string delimiter | print('Hello") | print("Hello") |
OCR Exam Tip: When looking for syntax errors, check every line for matching brackets, correct keywords, and proper structure. In OCR pseudocode, every
ifneeds anendif, everyforneeds anext, and everywhileneeds anendwhile.
Logic errors are harder to spot because the code runs without crashing — it just produces the wrong answer.
| Error | Description | Example |
|---|---|---|
| Wrong operator | Using < instead of > | if age < 18 when you mean if age > 18 |
| Off-by-one | Loop runs one too many or one too few times | for i = 0 to 10 when it should be for i = 0 to 9 for an array of 10 items |
| Wrong variable | Using the wrong variable in a calculation | total = total + count instead of total = total + score |
| Wrong initial value | Starting a counter at 1 instead of 0 | count = 1 when it should be count = 0 |
| Condition always true/false | A while loop that never terminates or never runs | while x > 0 when x starts at -1 and is never changed |
// This program should find the smallest number in an array
numbers = [5, 3, 8, 1, 4]
smallest = 0
for i = 0 to 4
if numbers[i] > smallest then
smallest = numbers[i]
endif
next i
print(smallest)
Errors:
smallest = 0 — should be smallest = numbers[0] (initialising to 0 may give wrong result if all numbers are positive... but actually here, no number is less than 0, so the logic error below is the main issue)numbers[i] > smallest — should be numbers[i] < smallest (we want the smallest, not the largest)Corrected:
numbers = [5, 3, 8, 1, 4]
smallest = numbers[0]
for i = 1 to 4
if numbers[i] < smallest then
smallest = numbers[i]
endif
next i
print(smallest)
Runtime errors cause the program to crash during execution.
| Error | Cause | Example |
|---|---|---|
| Division by zero | Dividing by a variable that is 0 | average = total / count when count = 0 |
| Index out of bounds | Accessing an array element that does not exist | names[10] when the array only has 5 elements |
| Type error | Performing an operation on incompatible types | "Hello" + 5 without type conversion |
| File not found | Trying to open a file that does not exist | openRead("missing.txt") |
OCR Exam Tip: When identifying errors, be specific. Do not just say "there is a logic error." Say "on line 5, the operator should be < instead of > because the program is supposed to find the smallest value."
The following program should ask the user for 5 numbers, then display the total:
total = 1
for i = 1 to 6
num = input("Enter a number: ")
total = total + num
next
print(total)
| Line | Error | Type | Correction |
|---|---|---|---|
| 1 | total = 1 should be total = 0 | Logic | Starting total at 1 gives an incorrect result |
| 2 | for i = 1 to 6 should be for i = 1 to 5 | Logic | The loop runs 6 times instead of 5 |
| 3 | num = input(...) should be num = int(input(...)) | Runtime/Logic | Input is read as a string, not a number |
| 5 | next should be next i | Syntax | The loop variable is missing (in OCR pseudocode) |
Corrected program:
total = 0
for i = 1 to 5
num = int(input("Enter a number: "))
total = total + num
next i
print(total)
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.