Identifying Errors
Revise Identifying Errors for Computer Science 0478 (O Level) — revision notes and instant AI marking. Free to start.
Identifying Errors
Syntax, Logic & Runtime Errors — plus Algorithmic Thinking
Big idea: every mistake a program makes falls into one of three buckets — it won't run (syntax), it runs but gives the wrong answer (logic), or it runs and then crashes (runtime) — and once you can tell those apart, you can find and fix almost any bug.
📋 Quick Overview
- Three error types exist: Syntax errors (grammar mistakes), Logic errors (wrong output), Runtime errors (crashes).
- Syntax errors stop a program from running at all — think typos, missing brackets, missing colons.
- Logic errors are the sneakiest — the program runs fine but gives the wrong answer. You need trace tables to hunt these down.
- Runtime errors happen while the program is running and cause it to crash — like dividing by zero or asking for input the code can't handle.
- Trace tables are your universal debugging tool — they help you catch all three error types by tracking variables step by step.
- Algorithmic thinking is the skill of breaking a problem into a precise, step-by-step sequence of instructions using abstraction and decomposition.
- Exam answers must use exact phrasing: "prevents the program from running" (syntax), "produces incorrect output" (logic), "causes the program to crash" (runtime).
1. Why This Topic Matters
Here's the thing nobody tells you when you start coding: you will never write a perfect program on the first try. Not you, not your teacher, not the engineers who built the app you're using right now. Every programmer spends huge chunks of their time hunting down bugs — and the first skill in hunting a bug is knowing what kind of bug you're even looking for.
Think of it like being a doctor. If a patient walks in, you don't just say "something's wrong" — you first work out: is this a broken bone (obvious, visible, stops them walking), an infection (they seem okay but something's off internally), or a sudden collapse (they were fine, then suddenly weren't)? Each needs a totally different fix. Programming errors work exactly the same way:
Cambridge IGCSE 0478 tests this constantly, especially in Paper 2, using trace tables and "dry runs" of code. Get comfortable with the three categories and the exact vocabulary examiners want, and this becomes one of the easiest topics to pick up marks on.
2. Syntax Errors
What Is a Syntax Error?
A syntax error is an error that breaks the grammatical rules of a programming language and stops it from running at all. Programming languages are strict about grammar in a way English isn't — English lets you get away with a missing comma, but Python will refuse to even start if you forget a colon.
Think of syntax like the rules of a board game. If the rulebook says "you must roll the dice before moving," and you try to move without rolling, the game simply won't let you take that turn. It doesn't matter how clever your strategy is — you broke the rule of the game itself, so nothing proceeds.
Let's look at a real example. Below is a Python program meant to generate a username from a person's first and last name. It's riddled with syntax errors — see if you can spot all four before reading on.
def generate_username(first_name, last_name) # ← missing colon
username = f"{first_name[0]}{last_name[0]}{last_name[:3]}"
return username
def main():
first_name = imput("Enter your first name: ") # ← "imput" not "input"
last_name = input("Enter your last name: ")
usdrname = generate_username(first_name, last_name) # ← typo, variable never matches
print(f"Here's a suggested username: {username}")
main # ← missing parentheses, function never called
2. Incorrectly spelt function name — imput should be input. Python has no function called imput, so it errors immediately.
3. Typo in variable name — the result is stored as usdrname but the print statement asks for username, which was never defined in main().
4. Missing parenthesis — writing main instead of main() refers to the function object but never actually calls it.
Here's the corrected version — notice every fix is small, but each one was blocking the whole program from running:
def generate_username(first_name, last_name):
username = f"{first_name[0]}{last_name[0]}{last_name[:3]}"
return username
def main():
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
username = generate_username(first_name, last_name)
print(f"Here's a suggested username: {username}")
main()
Identify the syntax error in this line of code and explain why it stops the program from running:
if age >= 18
print("You can vote")
A student writes: print("Total = " + total) where total is an integer, and the program crashes with a TypeError. Is this a syntax error? Explain your reasoning.
3. Logic Errors
What Is a Logic Error?
A logic error is where the code is syntactically correct and runs perfectly fine, but it produces an incorrect output or result. This is the trickiest error type because the program doesn't announce it — no crash, no error message, nothing. It just quietly gives you the wrong answer, and you might not notice unless you check the output carefully.
Imagine following a recipe that says "bake for 25 minutes at 180°C" when it should say 350°C. You'll follow every instruction perfectly, the oven will run, nothing will crash — but you'll pull out an undercooked cake. That's exactly what a logic error feels like in code: everything executes, but the underlying instruction was wrong.
How do you find logic errors? The main tool is the trace table — a table where you track the value of every variable, line by line, as the program executes. If the values diverge from what you expect at some point, that's exactly where your bug is hiding.
Let's examine a real case. This function is meant to calculate the area of a rectangle, but reject any length or width that isn't positive:
def calculate_area(length, width):
if length < 0 or width < 0: # ← the bug is here
raise ValueError("Length and width must be positive values.")
area = length * width
return area
The team tested it with two cases:
| Test | Test Data | Expected Outcome | Actual Outcome | Changes Needed? |
|---|---|---|---|---|
| 1 | Length = 5 Width = 5 |
"The area of the rectangle is approximately 25 square units." | "The area of the rectangle is approximately 25.00 square units." | N |
| 2 | Length = 10 Width = 0 |
"Length and width must be positive values." | "The area of the rectangle is approximately 0.00 square units." | Y — should not accept 0 as a positive input |
Test 2 exposes the logic error. A width of 0 should have been rejected as "not positive," but the code let it through and calculated an area of 0.00 anyway.
The comparison < 0 should be <= 0, so that a value of exactly 0 is correctly treated as not positive and gets rejected.
This is a perfect example of why logic errors are so dangerous in the real world — a program handling money, medicine dosages, or safety systems could quietly accept a value it should have blocked, and nobody would know until something went wrong downstream.
A program should print numbers 1 to 5 using this pseudocode:
FOR i = 1 TO 4
OUTPUT i
NEXT i
Identify the logic error and explain the fix.
A student wants to check if someone qualifies for a discount: they must be a student AND under 21. The code reads:
IF isStudent = TRUE OR age < 21 THEN
What's wrong, and how would you fix it?
4. Runtime Errors
What Is a Runtime Error?
A runtime error is where an error causes a program to crash while it is running. The code was grammatically fine (it passed the syntax check and started running), but partway through execution, it hits a situation it simply cannot handle — and the whole thing stops abruptly.
Think of it like driving a car that starts up perfectly fine (no syntax error there — the engine runs) but then hits a pothole so severe it blows a tyre and the car grinds to a halt. Nothing was wrong with starting the engine; the failure only happened once you were moving and hit a specific, unhandled situation.
Let's return to the rectangle area program, now fully corrected of its logic error, and test it with an unusual input:
def calculate_area(length, width):
if length < 0 or width < 0:
raise ValueError("Length and width must be positive values.")
area = length * width
return area
def main():
try:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = calculate_area(length, width)
print(f"The area of the rectangle is approximately {area:.2f} square units.")
except ValueError as error:
print(f"Error: {error}")
main()
| Test | Test Data | Expected Outcome | Actual Outcome | Changes Needed? |
|---|---|---|---|---|
| 1 | Length = 10 Width = 10 |
"The area of the rectangle is approximately 100 square units." | "The area of the rectangle is approximately 100.00 square units." | N |
| 2 | Length = "abc" Width = 0 |
"Program could not convert string to float, try again" | Program crashed | Y — should give an error message and ask the user to enter again |
Test 2 reveals the runtime error. Typing "abc" where a number is expected causes float(input(...)) to raise an error that isn't a ValueError from the rectangle logic — it's a conversion error the try/except block wasn't built to catch and recover from repeatedly. The program crashes instead of asking the user to try again.
while True:
try:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = calculate_area(length, width)
print(f"The area of the rectangle is approximately {area:.2f} square units.")
break # Exit the loop if successful
except ValueError as error:
print(f"Error: {error}")
print("Please enter positive values for length and width.")
Notice the fix isn't a one-line tweak like the syntax and logic examples — sometimes a runtime error needs a structural change, like adding a loop so the program recovers gracefully instead of dying.
A program stores 5 students' marks in an array indexed 0 to 4, then this line runs:
print(marks[5])
What type of error is this, and why does it happen?
Explain, in your own words, why a runtime error is different from a syntax error even though both can stop a program.
5. Comparing All Three Error Types
This is the single most useful table in this whole chapter — if you can rebuild this from memory, you're set for any "identify the error type" question.
| Feature | Syntax Error | Logic Error | Runtime Error |
|---|---|---|---|
| Does the program run? | No — never starts | Yes — runs fully | Starts, then stops partway |
| What goes wrong? | Breaks language grammar rules | Produces the wrong result | Hits an unhandled situation |
| Exam phrase | "Prevents the program from running" | "Produces incorrect output" | "Causes the program to crash" |
| How you find it | Read the error message / check code carefully | Trace tables, comparing expected vs actual output | Test with unusual/edge-case inputs |
| Example | Missing colon, misspelt keyword | Using < instead of <= | Dividing by zero, array index out of range |
A program is supposed to calculate average = total / count. When a user enters no data at all, count stays 0 and the program crashes. Which error type is this, and how do you know?
6. Algorithmic Thinking & Writing Algorithms
What Is Algorithmic Thinking?
Algorithmic thinking is the process of creating step-by-step instructions in order to produce a solution to a problem. It's the skill that sits underneath every single line of code you'll ever write — before you can code a solution, you need to think it through as a precise sequence of steps.
Algorithmic thinking leans on two big ideas you've probably met before:
Once every step has been identified through decomposition and abstraction, you can write a precise set of rules (an algorithm) that solves the problem reliably, every single time.
A set of traffic lights shows how algorithmic thinking leads to automation — a fixed sequence of rules (red → red+amber → green → amber → red) runs the same way, indefinitely, without a human deciding each time.
How to Approach Writing an Algorithm in an Exam
Writing algorithms under exam pressure is genuinely one of the hardest parts of this course — not because the logic is impossible, but because it's easy to lose track of what you're doing halfway through. Here's a practical approach that examiners specifically recommend:
2. Chunk the problem. Break your algorithm into labelled parts, e.g. "This part enters the grades," "this part calculates the total," "this part allocates the grade."
3. Order the chunks logically, then assemble the whole algorithm.
4. Use the exact variable names given in the question. This is a huge, easy-to-miss mark loser — if the question says the variable is called studentMark, don't rename it mark or score.
5. Comment your code. It helps the examiner follow your logic and often earns marks directly in the mark scheme.
A worked example of a plan for "read 30 students' marks, calculate their grade, and output a summary" might look like:
Part 1: Declare and initialise variables
Part 2: Loop to input marks for each subject
Part 3: Allocate grades for each student's subject
Part 4: Include a loop to repeat for all 30 students
Part 5: Output all necessary data
Explain, using an example, the difference between decomposition and abstraction when applying algorithmic thinking to a problem.
7. What to Memorise
8. Concepts Checklist
9. Exam Tips & Common Mistakes
What Examiners Are Really Looking For
- Correct identification of the error type using the correct terminology
- Precise location of the error (the exact line or expression)
- A clear explanation of the impact on the program's behaviour or output
- A working, testable fix — not just "add code here"
- Use of proper trace table structure when tracing variables
- Algorithms that use the exact variable names and data given in the question
- 6. Algorithmic Thinking & Writing Algorithms
- 9. Exam Tips & Common Mistakes
Read the full Identifying Errors notes free
That's the preview — create a free account to read the rest, plus flashcards and practice questions with instant AI marking. No credit card.
Unlock the full notes free →