GCSE · Computer Science · OCR · Spec J277
Logic errors
Your program runs. Nothing flagged, nothing crashed. Job done? Not if it just printed the wrong answer. That's a logic error, and it won't announce itself.
Computer Science · Debugging
Where did the 15 go?
The job: print the first five multiples of 3. Expected output: 3, 6, 9, 12, 15. Step through and compare that with what actually comes out.
Before you press Next: how many numbers do you think this program will actually print?
1 for i = 1 to 42 print(i * 3)3 next i
| i | i * 3 |
|---|---|
| 1 |
Output
Step 1: The loop starts with i = 1. Nothing printed yet.
Press Next one step at a time. Keep the expected output (3, 6, 9, 12, 15) in your head and check each number against it.
Errors · Three kinds
Syntax, runtime or logic?
Pick a description, then pick the type of error it describes. Commit first, then read why.
Still to sort
Syntax error (0)
The rules of the language haven't been followed. The translator flags it and the program won't run.
Runtime error (0)
Valid, logically correct code, but the environment or the input causes an error when it's run.
Where the line is: A runtime error and a logic error both get past the translator. The difference: with a runtime error, an error occurs while the program runs. With a logic error, the program runs without failing and gives the wrong result.
Logic error (0)
The program runs without failing, but it doesn't return the expected result.
Three kinds of error, and they behave very differently. Sort each one.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
No crash. No error message. Just the wrong answer. So how do you ever catch one?
What you need to know
- A logic error makes a program work incorrectly or unexpectedly.
- The program runs without failing, but it doesn't return the expected result.
Have a goPriya's calculator app works out 2 + 2, prints 5, and doesn't crash. "No error message, so no error!" she announces. Is she right?
No. She's got a logic error.
The program ran without failing, but it didn't return the expected result of 4. Running isn't the same as working.
- Logic errors are typically harder to find than syntax errors: the IDE can't pick them up or give clear error messages.
- Syntax errors break the rules of the language. The translator flags them, and the program won't run.
Have a goA program won't even start, and the translator is pointing at line 6. Syntax error or logic error?
A syntax error.
The translator flags syntax errors and the program won't run. A logic error gets past the translator, and the program runs.
- Runtime errors happen when valid, logically correct code is run, but the environment or the input causes an error.
- One common cause of a logic error: not correctly understanding what the program needed to do.
- Another: using the wrong logical operator in a selection statement, such as OR where AND was needed.
- A third: a loop that runs more or fewer times than intended.
Have a goA loop should print the numbers 1 to 10, but it prints 1 to 11. Which cause of logic error is this?
A loop running more times than intended.
It ran once too many. Nothing crashes, which is exactly why it slips through unless you check against the expected result.
- Debugging means finding and correcting errors in code. It kicks in when actual test results don't match expected results.
- Debug in order: find the error, read the error message, trace the code to see where it occurs, then fix and run to check.
The big picture
A logic error lets a program run without failing but stops it returning the expected result. Unlike a syntax error, nothing flags it, so it's typically harder to find. Common causes: misunderstanding what the program needed to do, the wrong logical operator in a selection statement, and loops that run more or fewer times than intended. You notice one when actual results don't match expected results, then debug: find it, read any error message, trace the code to see where it occurs, fix it and run to check.
Key points
Worked example
Problem
A ticket machine program should print "Ticket" exactly 3 times. When it's tested, it prints "Ticket" 4 times. Here's the code: count = 0 while count <= 3 print("Ticket") count = count + 1 endwhile Debug it.
⚠ Watch out
Thinking a program that runs without an error message must be working. A logic error runs without failing and still gives the wrong result, so you only catch it by comparing actual results with expected results.
Memory hook
Syntax: won't run. Runtime: errors while running. Logic: runs to the end, wrong answer. The quiet one is the one to watch.
Check yourself
Cover the page. For each of the three kinds of error, can you say whether the program runs at all, and how you'd notice something was wrong?
Flashcards
(12)What is a logic error?
Does a program with a logic error run?
Why are logic errors typically harder to find than syntax errors?
What is a syntax error?
What is a runtime error?
Which cause of logic error comes from the programmer's understanding?
What logic error can hide in a selection statement?
What logic error can a loop cause?
Define debugging.
What test outcome tells you it's time to debug?
The four debugging steps, in order?
Why run the code again after fixing an error?
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 OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 3 November 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.