DoAssignment study guide
A4.1 · Find and correct syntax, logic, and run-time errors
Learn to find and correct syntax, logic, and run-time errors through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Programming Concepts and Skills
ICS3U study topic: A4.1
A program can fail in different ways. It might not follow the language’s writing rules, it might run but produce the wrong answer, or it might stop while running because an operation cannot be completed. A4.1 asks you to find and correct syntax, logic, and run-time errors. A steady approach is to identify what the program is meant to do, inspect its inputs and steps, and trace the values to the expected output. The examples use Python 3. Other classrooms may use a different programming language, with different syntax.
What you will learn
- Tell the difference between syntax, logic, and run-time errors.
- Use the program’s intended task and its inputs to help locate an error.
- Trace values through a short program and check whether its output is correct.
- Make a focused correction and explain why it addresses the error.
Three ways a program can go wrong
A syntax error means that the program’s instructions do not follow the rules of the language. For example, Python uses a colon at the end of an if line. If it is missing, Python cannot read the statement as written. Correct syntax is necessary before the program can proceed, but it does not guarantee that the program will do the right task.
A logic error is a mistake in the steps or condition. The program can run, but its result does not match the task. For instance, a program meant to calculate the total of two values may accidentally subtract them. The language accepts the instruction, but the instruction is not the intended one.
A run-time error occurs while a program is running. The instructions may be syntactically valid, but a particular operation cannot be completed for the values the program reaches. Dividing by zero is one example. A run-time error can depend on the input, so checking more than one suitable input can help reveal it.
- Syntax: the language cannot accept the instructions as written.
- Logic: the instructions run but do not achieve the intended result.
- Run-time: an operation fails during a particular run.
A practical way to debug
Debugging means finding and correcting a problem in a program. Begin by stating the intended task in plain language. Then identify the inputs, the processing that should happen, and the expected output. This input-process-output description gives you something specific to compare with the code.
Next, check whether the code can be read according to the language’s syntax. Look closely at punctuation, spelling, and indentation where the language requires it. If the program can run, trace its values one instruction at a time. Write down the value of a variable after each assignment, and compare the final output with what the task requires.
Use a small, relevant test input with an output you can work out by hand. For a calculation, choose values that make the intended result clear. For a condition, try inputs that should make the condition true and false. These are planned checks, not a claim that code has been executed. When you find a mismatch, make a focused correction and trace the corrected steps again.
- Describe the intended task before changing code.
- Trace values from input through processing to output.
- Check that a correction fixes the stated task, not just one example.
Check the result against the task
A useful trace records the input, each important variable change, and the output the instructions should produce. For example, if a variable starts at 4 and the program adds 3, its new value is 7. Recording this change makes it easier to see whether a later instruction uses the right value.
When checking a condition, compare the condition with the task’s wording. If the task says a person qualifies at 16 or older, the condition must include 16. Testing 15 and 16 helps check the boundary. Do not assume that a program is correct just because one input gives a plausible answer.
After correcting an error, read the relevant line and trace it again. Ask: Does the line follow the language’s rules? Does it carry out the intended step? Can it fail for the input being considered? These questions help distinguish the three error types and keep the correction grounded in the program’s purpose.
- Trace intermediate values, not only the final answer.
- Choose test inputs that check the task’s important cases.
- A correction should be explained by the intended behaviour.
Trace for the pass condition
| Input age | Condition age >= 16 | Expected branch |
|---|---|---|
| 15 | False | Not yet |
| 16 | True | Pass allowed |
Worked example
Correcting a syntax error
A program should display whether a supplied temperature is above 20. The original if line is missing Python punctuation, so the program cannot be read as intended. Identify the error and trace a corrected version for the input 23.
Python 3
temperature = 23
if temperature > 20:
print("Above 20")
else:
print("20 or below")- Identify the task and inputThe input is the temperature 23. The program should compare it with 20 and display the appropriate message.
- Locate the syntax problemIn Python 3, an if line needs a colon before its indented body. The original line omitted that colon. Adding it makes the conditional statement follow Python’s syntax.
- Trace the corrected instructionsThe value of temperature is 23. The comparison 23 > 20 is true, so the indented print instruction displays the first message. This is a trace of the code, not a report of running it.
Answer: The corrected program displays: Above 20
Check: The syntax correction allows the if statement to be written in the required Python form. The comparison also matches the stated task.
Worked example
Correcting a logic error
A club gives a pass to anyone aged 16 or older. The original condition uses age > 16, which leaves out a person who is exactly 16. Correct the condition and trace the program for age 16.
Python 3
age = 16
if age >= 16:
print("Pass allowed")
else:
print("Not yet")- Translate the ruleThe phrase “16 or older” includes 16 itself. The condition must therefore accept values equal to 16 as well as values greater than 16.
- Correct the logicThe original condition age > 16 is valid Python syntax, but it does not match the rule at the boundary. Use age >= 16 so that equality is included.
- Trace the inputFor age 16, the corrected comparison 16 >= 16 is true. The program displays “Pass allowed.” For age 15, the comparison would be false, which also matches the stated rule.
Answer: For age 16, the corrected program displays: Pass allowed
Check: The corrected condition includes 16 and rejects values below 16, matching “16 or older.”
Worked example
Correcting a run-time error
A program calculates how many points each player receives when a total is shared equally. The original calculation divides by the number of players. If that input is zero, the division cannot be completed. Correct the program so it checks for this case before dividing. Trace it for 12 points and 3 players.
Python 3
total_points = 12
players = 3
if players > 0:
print(total_points / players)
else:
print("Player count must be positive")- Find the operation that can failThe calculation divides total_points by players. When players is zero, there is no valid division for this calculation, so the program needs to check the input before performing it.
- Guard the calculationCheck whether players is greater than zero first. Only the true branch performs the division. The other branch displays a message rather than attempting the invalid operation.
- Trace the supplied inputHere, players is 3, so players > 0 is true. The calculation is 12 divided by 3, giving 4 points per player. If players were 0, the program would take the other branch and display its message without dividing.
Answer: For 12 points and 3 players, the program displays: 4.0
Check: The check prevents division by zero for this input. For a positive player count, the program carries out the requested division.
Common mistakes and how to avoid them
Calling every problem a syntax error.
Correction: Ask whether the instructions cannot be read, run but give the wrong result, or fail during a run. These clues distinguish syntax, logic, and run-time errors.
Changing several lines at once without tracing them.
Correction: Make a focused correction and trace the relevant values again. This helps you see whether the change addresses the identified problem.
Checking only an ordinary input and missing a boundary value.
Correction: When a rule uses words such as “at least,” check the exact boundary as well as a value just below it.
Assuming that valid syntax means the program is correct.
Correction: Syntax only concerns whether the language can read the instructions. Check the result against the intended task to find logic errors too.
Lesson summary
- A syntax error breaks the writing rules of the programming language.
- A logic error lets the program run but gives a result that does not match its task.
- A run-time error occurs when an operation cannot be completed during a run.
- State the task, identify inputs, trace values, check expected output, and make a focused correction.
- Python 3 syntax is used here; other classroom languages may express the same steps differently.
Check your understanding
Question 1
A program meant to add two numbers uses subtraction instead. It runs and displays a result. What type of error is this?
- Syntax error
- Logic error
- Run-time error
- correctIndex 2
Show answer and explanation
Logic error
The instructions are accepted and produce a result, but the operation does not match the intended task. That is a logic error.
Question 2
A Python 3 program attempts to divide a value by zero while running. What type of error is being addressed?
- Syntax error
- Logic error
- Run-time error
- correctIndex 2
Show answer and explanation
Run-time error
The failure occurs when the program reaches an operation it cannot complete during that run, so it is a run-time error.
Question 3
A rule says that a learner qualifies at age 14 or older. Which condition includes age 14?
- age > 14
- age >= 14
- age < 14
- correctIndex 2
Show answer and explanation
age >= 14
The greater-than-or-equal comparison includes equality, so age 14 qualifies. A greater-than comparison alone would exclude 14.
Key terms
- Syntax error
- A problem where instructions do not follow the writing rules of the programming language.
- Logic error
- A mistake in the program’s steps or condition that leads to a result different from the intended one.
- Run-time error
- A problem that occurs while the program is running because an operation cannot be completed.
- Trace
- A written record of how values and instructions change as you follow a program.
- Debugging
- Finding and correcting errors in a program.
Continue through ICS3U
View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons
- A3.2 · Write subprograms with parameters and appropriate scope
- A4.2 · Use clear names, indentation, and comments
- A1.1 · Use variables and constants with appropriate data types
- A1.2 · Explain binary, hexadecimal, and character encoding
- A1.3 · Use arithmetic and string assignment statements
- A1.4 · Use Boolean, comparison, and arithmetic operators correctly
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A4.1. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.