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 = 0
3 for i in range(0, 3):
4 total = total + scores[i]
5 print(total)
iscores[i]total
0

Output

 

Step 1: The four scores are stored and total starts at 0. Nothing unusual yet.

1 / 5

Press Next to move one line at a time. Keep the expected answer, 18, in your head the whole way.

Step 1 of 5: The four scores are stored and total starts at 0. Nothing unusual yet..

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.

6 of 6 still to sort.

Logic errors have two cousins. Sort each situation into the right family — then read why it belongs there.

Computer Science · Programming

It ran without an error message. So it's fine… right?

Sam's program runs from the first line to the last. No red text, no error message, nothing flagged by the IDE.

Which is closest to what you'd say about Sam's program right now?
How sure are you?

Computer Science · Debugging

Find the line that lets everyone in

This program should only let ages 11 to 16 join the junior club. Tested with age 40, it printed "You can join the junior club" — with no error at all. Which line holds the logic error?

A program with one logic error — which line goes wrong?

Computer Science · Testing

Write the test that catches it

How a test table works: each row records a test number, a description of the test, the test data (the input), the expected output, the actual output and — if the test failed — how it was fixed. A test fails when the expected output doesn't match the actual output. Three kinds of test data: • Normal — valid data the program should accept. • Boundary — values on the edge of being valid. • Erroneous — data the program should not accept, because accepting it would cause an error. Test 3 — Description: the user cannot enter a negative number of days. Actual output: the payment was accepted and change was given.

A program takes payment for a number of days. Using Test 3 above, write the row: the test data, the type of test data, the expected output and whether the test passed. Then explain what the result shows and how the program should be fixed. [6 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1Logic error = the program runs, but the result is wrong.
2The IDE can't detect a logic error, so you must compare expected output with actual output.
3A test fails when the expected output and the actual output don't match.
4Debugging: trace through the code to find where the error occurs, fix it, then run the code again to check the update works.
5Normal data should be accepted; boundary data is on the edge of being valid; erroneous data should not be accepted.

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?
An error that makes a program work incorrectly: it runs without failing, but does not return the expected result.
Why are logic errors harder to find than syntax errors?
The IDE can't pick them up or produce a clear error message — the program runs as normal.
Name four common causes of logic errors.
Not understanding what the program needed to do; the wrong logical operator in a selection statement; missing or wrongly positioned parts of a calculation; loops that run more or fewer times than intended.
What is a syntax error?
An error where the code doesn't follow the rules of the language. The translator flags it, and the program will not run.
What is a runtime error?
An error that occurs while the program runs: the code is valid and logically correct, but the input or the environment causes an error.
What is debugging?
The process of finding and correcting errors in program code.
How do you find WHERE a logic error is?
Trace through the code to detect where the error occurs — follow the values until they first go wrong.
You've fixed a bug. What must you do next?
Run the code again to check that the update works — the actual output should now match the expected output.
What does a test table track?
Expected versus actual outcomes: usually test number, description, test data (input), expected output, actual output, and how it was fixed if the test failed.
When does a test fail?
When the expected output does not match the actual output.
What is normal test data?
Valid data that the program should accept.
What is boundary test data?
Values on the edge of being valid.
What is erroneous test data?
Data the program should not accept, because accepting it would cause an error.
A program let a user add a game review that made a stream too long. What was added to fix it?
A while loop that checks whether total_time is less than stream_seconds before letting the user add another game review.

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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.