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 4
2 print(i * 3)
3 next i
ii * 3
1

Output

 

Step 1: The loop starts with i = 1. Nothing printed yet.

1 / 8

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.

Step 1 of 8: The loop starts with i = 1. Nothing printed yet..

Exam line: A logic error is noticed when the actual result doesn't match the expected result, and found by tracing through the code.
Watch out: Every line here is valid code, and the program runs without failing. That's exactly why this error is so easy to miss.

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.

6 of 6 still to sort.

Three kinds of error, and they behave very differently. Sort each one.

Errors · Why they hide

It runs, so it works?

Sam's program runs all the way through. Nothing crashes, and the IDE hasn't flagged a single line.

What can Sam say about the program right now? Pick the idea closest to yours.
How sure are you?

Logic errors · The wrong operator

Find the line that lets everyone in

This cinema program should print "Teen ticket" only for ages 13 to 19. A tester enters 40 and the program prints "Teen ticket". Every line is valid code. Which line holds the logic error?

The cinema ticket program — which line goes wrong?

Debugging · The method

Put the debugging steps in order

The order you carry out the steps when debugging a program during development

1 · First step4 · Last step
  1. Fix the error and run the code to check the update works

  2. Trace through the code to detect where the error occurs

  3. Find the error

  4. Read the error message

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

1Logic error: the program runs without failing, but works incorrectly or unexpectedly.
2Syntax error: language rules broken, flagged by the translator, program won't run. Runtime error: valid, logically correct code, but the environment or input causes an error.
3Logic errors are typically harder to find than syntax errors, because the IDE can't detect them or give clear error messages.
4Common causes: misunderstanding what the program needed to do, the wrong logical operator in a selection statement, and loops running more or fewer times than intended.
5When actual results don't match expected results, debug: find, read any error message, trace to see where, fix and run to check.

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?
An error that causes a program to work incorrectly or unexpectedly.
Does a program with a logic error run?
Yes. It runs without failing, but it doesn't return the expected result.
Why are logic errors typically harder to find than syntax errors?
The IDE can't pick them up or produce clear error messages about them.
What is a syntax error?
The rules of the language haven't been followed. The translator flags it, and the program won't run.
What is a runtime error?
Valid, logically correct code hits an error when it's run, because of the environment it's run in or the input provided.
Which cause of logic error comes from the programmer's understanding?
Not correctly understanding what the program needed to do.
What logic error can hide in a selection statement?
Using the incorrect logical operator, e.g. OR where AND was needed.
What logic error can a loop cause?
The loop executes more or fewer times than intended.
Define debugging.
The process of finding and correcting errors in program code.
What test outcome tells you it's time to debug?
The actual results of a test don't match the expected results.
The four debugging steps, in order?
Find the error; read the error message; trace through the code to detect where it occurs; fix it and run the code to check.
Why run the code again after fixing an error?
To check that the update works.

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 OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.