Program errors (Edexcel GCSE Computer Science): Revision Notes
Programme errors
Understanding how to spot and correct different types of programming errors is a crucial skill for any programmer. When you write code, three main types of errors can occur, and each one needs a different approach to fix.
What are programme errors?
Programme errors are mistakes in your code that prevent it from working as intended. Learning to identify and fix these errors (called debugging) is an essential part of programming. The three main categories of errors you'll encounter are syntax errors, runtime errors, and logic errors.
Syntax errors
Syntax errors occur when you make mistakes with the grammar rules of the programming language you're using. Think of it like making spelling or grammar mistakes when writing English - the computer can't understand what you're trying to say.
Common examples of syntax errors
Here are some typical syntax errors you might make:
Worked Example: Common Syntax Errors
for count in range (1, 10) # Missing colon (:)
print count # Missing parentheses ()
If (index > 10): # 'If' should be lowercase 'if'
print ("Count is high")
Each line shows a different syntax error that would prevent the code from running.
How to spot syntax errors
Most code editors and Integrated Development Environments (IDEs) will highlight syntax errors for you automatically. They often show them with:
- Red underlines
- Different coloured text
- Error symbols in the margin
This makes syntax errors usually the easiest type to find and fix quickly.
Exam Tip for Syntax Errors
Always check your code carefully for missing colons, brackets, and correct spelling of keywords. These are the most common syntax errors in exams.
Runtime errors
Runtime errors are more sneaky - your code looks correct and doesn't show any syntax errors, but when you actually run the programme, it crashes and stops working.
Understanding runtime errors
When a runtime error occurs:
- The programme starts running normally
- It crashes at the point where the error happens
- The computer gives you an error message explaining what went wrong
Example of a runtime error
Worked Example: TypeError Runtime Error
myList = [1, 2, 3, 4]
newList = []
for number in myList:
newList.append (number + "A") # TypeError occurs here
This code will produce a TypeError because you can't add a number (integer) to a letter (string). The error message might look like:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Best Practice for Runtime Errors
Since runtime errors are harder to spot just by looking at code:
- Always test your programmes thoroughly
- Run your code frequently as you write it
- Read error messages carefully - they tell you exactly what went wrong and often which line caused the problem
Logic errors
Logic errors are the trickiest type because your programme runs without crashing, but it doesn't do what you intended. The results are wrong or unexpected.
Understanding logic errors
With logic errors:
- The code follows correct syntax rules
- The programme runs without crashing
- But the output or behaviour is incorrect
Example of a logic error
Worked Example: Off-by-One Logic Error
num = 3
count = 0
total = 0
while (count < num): # Should use <= instead of <
total = total + count
count = count + 1
print (total)
This programme should add up numbers 0 to 3 to give a total of 6, but it actually outputs 3 because the condition count < num stops too early.
Finding logic errors
Techniques for Finding Logic Errors
Logic errors are easier to find when you:
- Use a debugger in your IDE to step through code line by line
- Add print statements to see what values your variables have
- Trace through your code manually with pen and paper
- Test with different inputs to see if the pattern of wrong results gives you clues
Best Practice for Logic Errors
- Plan your algorithm carefully before coding
- Test your programme with several different inputs
- Use debugging tools to watch how your variables change
- Walk through your code step by step to check the logic
Practical debugging strategy
When you encounter errors, try this systematic approach:
- Read any error messages carefully - they often tell you the line number and type of error
- Check for syntax errors first - these are quickest to fix
- Test your programme with simple inputs to see if it runs
- Use print statements or a debugger to check your logic
- Fix one error at a time - don't try to fix everything at once
Key Points to Remember:
- Syntax errors: Grammar mistakes in your code - usually highlighted by editors and easy to spot
- Runtime errors: Code looks correct but crashes when running - always read the error messages carefully
- Logic errors: Programme runs but gives wrong results - use debuggers and testing to find these
- Test frequently: Run your code often as you write it to catch errors early
- Error messages are helpful: They tell you exactly what went wrong and usually which line to check