DoAssignment study guide
B4.5 · Debug using traces and variable-state output
Learn to debug using traces and variable-state output through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Software Development
ICS3U study topic B4.5
What you will learn
- Explain how a trace helps locate a program error.
- Record how variable values change as statements are followed in order.
- Use a debugger to inspect and display variable state at useful points.
- Compare traced behaviour with a program’s intended result.
Trace the state, not just the final display
- Follow statements in order and update the trace after assignments.
- Use the current state at each output or decision.
- Find the first state that conflicts with the program’s intention.
Use a debugger to inspect variable state
- Pause at a useful point and inspect relevant variable values in the debugger.
- Compare values before and after the statement under investigation.
- Use debugger observations to locate a mismatch, then check the responsible statement.
A focused debugging routine
- State the intended result for a specific input.
- Use traces and debugger variable-state output to investigate the first mismatch.
- After a correction, check the relevant state changes and expected result again.
Keep observations relevant
- Record only the state that helps explain the behaviour.
- Use the trace to connect debugger observations from different points.
- Follow the program’s order before concluding which statements ran.
Trace for Example 2
| Pass | amount | total after update |
|---|---|---|
| Before loop | — | 0 |
| 1 | 2 | 2 |
| 2 | 5 | 7 |
| 3 | 3 | 10 |
Worked example
Example 1: Locate an incorrect total
Python 3
item_price = 12
total = item_price
total = total - 4
print(total)- Trace the assignmentsThe input is item_price = 12. The first assignment makes total equal to 12. The next assignment subtracts 4, so total becomes 8. That is the first state that conflicts with the intended total: the fee should be added.
- Inspect the update in the debuggerPause near the update and inspect total and item_price. Before the update, the trace predicts total is 12 and item_price is 12. After the update, it predicts total is 8. These values point to the subtraction statement as the source of the mismatch.
- Check the intended updateThe update should add 4. With that correction, the traced total becomes 16, matching the stated intention. The expected display from the original code is 8; this is derived from the shown statements, not from a claimed run.
Worked example
Example 2: Inspect a running total
Python 3 list indexes are zero-based; the loop uses these values in order.
Python 3
amounts = [2, 5, 3]
total = 0
for amount in amounts:
total = total + amount
print(total)- Note the starting stateThe input list contains 2, 5, and 3 at Python indexes 0, 1, and 2. Before the loop, total is 0. The loop takes each amount in order and adds it to the current total.
- Follow each updateAfter the first update, total is 2. After the second, it is 7. After the third, it is 10. If the debugger pauses after each update, these are the total values to compare with the trace.
- Derive the final displayThe print statement follows the loop, so it displays the final total, 10. If a debugger observation differs from the trace at a pause point, inspect the update that has just been followed and the amount used there.
Worked example
Example 3: Check a choice in the debugger
For score 7, the shown condition is false, so the else branch is followed.
Python 3
score = 7
if score > 10:
print("small")
else:
print("large")- Trace the conditionThe input makes score equal to 7. The condition asks whether score is greater than 10. That is false, so the program follows the else branch and displays "large". This conflicts with the intended result, "small".
- Inspect the decision pointPause just before the condition and inspect score. The trace predicts that it is 7. Then follow the decision and check which branch is taken. If score is 7 and the else branch runs, the condition does not express the stated rule.
- Compare with the intended ruleThe condition should ask whether score is less than 10. For score 7, that condition is true, so the first branch displays "small". This correction follows from the stated rule and the input.
Common mistakes and how to avoid them
Lesson summary
- A trace records relevant variable values as statements are followed in order.
- A debugger can pause at selected points so you can inspect variable state and compare it with your trace.
- Use the first mismatch to guide investigation, then check the corrected state and expected result.
Check your understanding
Question 1
- 3
- 2
- 5
- 6
Show answer and explanation
Question 2
- At the start of an unrelated section
- Just after the update being investigated
- Only after the program has finished
- Before the input is received
Show answer and explanation
Question 3
- Inspect that assignment and the values it uses
- Change the final display text without checking the update
- Assume the input must be incorrect
- Record unrelated variables instead
Show answer and explanation
Key terms
- Debugging
- Finding and correcting problems in a program.
- Trace
- A record of program steps and relevant variable values as the steps are followed in order.
- Variable state
- The value a variable holds at a particular point in a program.
- Debugger
- A tool for pausing a program and inspecting its state as its statements are followed.
Continue through ICS3U
View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons
- B4.4 · Compare expected and actual results in a test plan
- B4.6 · Communicate project status clearly
- B1.1 · Choose problem-solving strategies for different tasks
- B1.2 · Solve problems alone and in a team
- B1.3 · Plan a solution using an input–process–output model
- B2.1 · Adapt a program template to a specification
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation B4.5. 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.