Identify Syntax & Logic Errors in Testing (OCR GCSE Computer Science): Revision Notes
Identify Syntax & Logic Errors in Testing
When writing programmes, two common types of errors arise: syntax errors and logic errors. Understanding these errors is essential for effective debugging and testing.
Syntax Errors
Syntax errors occur when the rules (grammar) of the programming language are broken.
These errors prevent the programme from running and must be corrected before the programme can execute.
Examples include:
- Missing brackets or colons.
- Incorrectly spelt commands or variables.
- Misplaced operators or indentation issues.
Example
print("Hello World) # Syntax Error: Missing closing quotation mark.
Logic Errors
- Logic errors happen when the programme runs but produces incorrect results due to flaws in the logic.
- The programme may not crash, but it won't work as intended, producing unexpected outputs.
- These errors can be harder to detect because they do not stop the programme from running.
Example
def calculate_area(length, width):
return length + width # Logic Error: Should multiply, not add
Logic Gates and Truth Tables
Logic gates are used in computer circuits to perform simple logic operations. They work with binary values, where inputs and outputs are either 0 (false) or 1 (true). Each type of logic gate has a truth table that shows the output for every possible input combination.
Truth Tables for Common Gates:
| NOT Gate | OR Gate | AND Gate | |||
|---|---|---|---|---|---|
| Input A | Output | Input A | Input B | Output | Input A |
| 0 | 1 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 1 | ||
| 1 | 1 | 1 | 1 |
Combining Logic Gates:
Complex logic can be created by combining multiple gates. Below is an example of combining a NOT gate with an AND gate.
Circuit
- The NOT gate takes input
pand producesNOT p. - The output of
NOT pand inputqare passed into an AND gate.
Truth Table for the Combined Circuit
| p | q | NOT p | (NOT p) AND q |
|---|---|---|---|
| 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 0 |
Explanation
- Step 1: Work out the value of
NOT p(the negation ofp). - Step 2: Use the result of
NOT pand the value ofqas inputs to the AND gate. - Step 3: The final column shows the output of the entire circuit:
(NOT p) AND q.
Identifying and Resolving Errors in Logic and Syntax
When designing programmes and circuits:
- Syntax errors prevent execution and must be corrected for the programme to run.
- Logic errors produce incorrect outputs despite the programme running smoothly.
- Use truth tables to verify logic in circuits and ensure that complex logic gates work as intended.
Key Points to Remember
- Syntax errors break the rules of the programming language and stop a programme from running.
- Logic errors cause a programme to run but produce incorrect or unexpected output.
- Use truth tables to verify the logic of circuits and test programmes effectively.