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 |
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)Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.