GCSE · Computer Science · Edexcel · Spec 1CP2
Logic errors
Most bugs make a fuss: an error message, a program that won't start. This one doesn't. The program runs perfectly, finishes without complaint — and hands you the wrong answer.
Computer Science · Programming
The program that runs perfectly — and gets it wrong
This program should add up four scores. Work out the right answer yourself first, then step through and watch what the program actually does.
Expected output: 4 + 7 + 2 + 5 = 18. Will the program print 18?
1 scores = [4, 7, 2, 5]2 total = 03 for i in range(0, 3):4 total = total + scores[i]5 print(total)
| i | scores[i] | total |
|---|---|---|
| 0 |
Output
Step 1: The four scores are stored and total starts at 0. Nothing unusual yet.
Press Next to move one line at a time. Keep the expected answer, 18, in your head the whole way.
Computer Science · Programming
Syntax, runtime or logic?
Which type of error does each situation show?
Still to sort
Syntax error (0)
The code breaks the rules of the language. The translator flags it, and the program will not run.
Where the line is: This is the only one of the three where the program never gets to run at all.
Runtime error (0)
The code follows the rules and is logically correct, but the input or the environment it runs in causes an error while it runs.
Where the line is: Unlike a logic error, an error actually occurs while the program is running.
Logic error (0)
The program runs without failing, but it does not return the expected result.
Where the line is: The translator can't see it and no error occurs — the result is simply wrong.
Logic errors have two cousins. Sort each situation into the right family — then read why it belongs there.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
That's a logic error — and the only way to catch it is to know what the answer should have been.
What you need to know
- A logic error lets a program run without failing, but it does not return the expected result.
- Logic errors are harder to find than syntax errors: the IDE can't pick them up or give a clear error message.
- Common causes include: not understanding what the program needed to do, the wrong logical operator in a selection statement, missing or wrongly positioned parts of a calculation, and loops that run more or fewer times than intended.
- A syntax error breaks the language's rules and stops the program running; a runtime error occurs while it runs, caused by the input or the environment.
- Test with normal, boundary and erroneous data, record expected and actual output in a test table, then debug by tracing, fixing and re-running.
The big picture
A logic error lets a program run all the way to the end without failing, but it does not give the expected result. The IDE cannot pick it up, so you catch it by testing — comparing expected output with actual output — and find it by tracing through the code.
Key points
Worked example
Problem
A program should work out the average of three marks using the line average = mark1 + mark2 + mark3 / 3. It is tested with the marks 6, 9 and 12. Find and fix the logic error.
⚠ Watch out
Thinking a program is correct because it ran without an error message. A logic error lets the program run perfectly — you only catch it by comparing the actual output with the output you expected.
Memory hook
Syntax won't start. Runtime trips while it's running. Logic runs all the way to the end — and gives you the wrong answer with a straight face.
Check yourself
A program runs with no error message but prints 12 when you expected 15. What type of error is this, why didn't the IDE warn you, and what would you do next to find it?
Flashcards
(14)What is a logic error?
Why are logic errors harder to find than syntax errors?
Name four common causes of logic errors.
What is a syntax error?
What is a runtime error?
What is debugging?
How do you find WHERE a logic error is?
You've fixed a bug. What must you do next?
What does a test table track?
When does a test fail?
What is normal test data?
What is boundary test data?
What is erroneous test data?
A program let a user add a game review that made a stream too long. What was added to fix it?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 29 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.