GCSE · Computer Science · OCR · Spec J277
Syntax errors
One wrong letter. That's all it takes for a whole program to refuse to start. Let's see exactly what happens, and how to fix it fast.
Computer Science · Programming
Press Run and watch what doesn't happen
Attempt 1 has one typo on line 4. Attempt 2 is the same program after the fix. Step through both and keep an eye on the Output box.
Lines 2 and 3 are perfectly fine. Will 'Starting...' get printed before the typo on line 4 causes trouble? Decide, then step.
1 # Attempt 12 print("Starting...")3 name = "Sam"4 iff name == "Sam":5 print("Hi Sam!")6 # Attempt 2: after the fix7 print("Starting...")8 name = "Sam"9 if name == "Sam":10 print("Hi Sam!")
Variables
Output
Step 1: You press Run on Attempt 1. Made your prediction? Keep your eyes on the Output box and the Variables panel.
Use Next to step. Steps 1 to 4 are Attempt 1, step 5 is the fix, and Attempt 2 runs from step 6.
Diagnose it
Which kind of error is it?
Pick a situation, then choose syntax error or logic error.
Still to sort
Syntax error (0)
The language's rules are broken, so it won't run.
Where the line is: If the program ran at all, it isn't this.
Logic error (0)
It runs, but the result isn't what you expected.
Where the line is: If the translator flagged it, it isn't this.
Six things that went wrong. Put each one where it belongs.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Break the language's rules and the program won't even start. Here's how to spot the slip and fix it.
What you need to know
- A syntax error happens when your code breaks the rules of the programming language.
- The translator is what flags a syntax error.
- A program with a syntax error won't run at all, not even the lines before the mistake.
Have a goYour mate's 100-line game has a misspelt keyword on line 99. "Relax," they say, "the first 98 lines will still run." Will they?
No. Not one line runs.
A misspelt keyword breaks the rules, so it's a syntax error. A program with a syntax error won't run at all, so line 99 holds up lines 1 to 98 as well.
- Common causes are spelling or typing slips, such as a misspelt keyword or a missing bracket.
- Most IDEs highlight syntax errors when you try to run your code.
- A logic error is different: the program runs, but it doesn't give the result you expected.
- Logic errors are typically harder to detect, because the IDE can't pick them up.
Have a goIf you had to choose, would you rather hunt for a syntax error or a logic error? Pick one, then say why.
A syntax error.
A syntax error gets flagged by the translator. A logic error lets the program run and the IDE can't pick it up, so you have to find it yourself.
- Debugging is the process of finding and correcting errors in program code.
- Start debugging by finding the error, reading the error message, then tracing through the code to see where it occurs.
- Finish by fixing the error and running the code again to check the update works.
Have a goPriya spots her typo, fixes it, then shuts her laptop. "Done!" What has she skipped?
Running the code again to check the fix works.
The fix isn't proven until the program runs. Skipping the check means hoping the fix worked rather than knowing it did.
The big picture
A syntax error happens when code breaks the rules of the programming language. The translator flags it, and a program with a syntax error will not run at all. Common causes are spelling or typing slips, and most IDEs highlight syntax errors when you try to run your code. A logic error is different: the program runs but doesn't return the expected result, and because the IDE can't pick it up, it is typically harder to detect. Debugging means finding and correcting errors: find the error, read the error message, trace through the code to see where it occurs, fix it, then run the code to check the update works.
Key points
Worked example
Problem
Leo presses Run on this three-line program and gets an error message instead of any output. Debug it. for lap in range(1, 4) print("Lap", lap) print("Race over!")
⚠ Watch out
Thinking a program with a syntax error will run up to the faulty line and then stop. It feels right, because you read code from the top down. But a program with a syntax error won't run at all, so even a perfect line 1 never runs.
Memory hook
Syntax error? It won't start. Logic error? It starts, but gets it wrong.
Check yourself
A program has a mistyped keyword on its very last line. How much of it runs, what kind of error is it, what flags it, and how would you debug it?
Flashcards
(11)What is a syntax error?
What flags a syntax error?
How much of a program runs if it contains a syntax error?
Give the common causes of syntax errors.
When do most IDEs highlight syntax errors?
What is a logic error?
Why are logic errors typically harder to detect than syntax errors?
What is debugging?
Once you've found an error, what do you do before tracing through the code?
Why do you trace through the code when debugging?
What is the final step of debugging, and why?
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.