DoAssignment study guide
A4.4 · Trace Variables and Debug Program Logic
Learn to trace program state to find errors through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Programming Concepts and Skills
ICS3U study topic A4.4: Follow stored values through a program and compare the result with its intended behaviour.
What you will learn
- Explain program state in plain language.
- Trace changes to variables and list elements, one statement at a time.
- Compare a traced result with the intention to locate a likely error.
- Use a small input and a trace table to explain your reasoning.
What to record while tracing
- State means the values currently stored in variables and data.
- Follow statements in order; update a value only when a statement changes it.
- Compare the traced result with the stated intention.
A careful method for locating a mismatch
- State the intended result before tracing.
- Choose an input with an expected result you can verify by hand.
- Trace the code as written before suggesting a correction.
Keep the reasoning checkable
- Small inputs make tracing easier to check.
- Record output when it is produced, not only at the end.
- A trace describes the shown input; it is not a claim that the code was executed.
Trace for the loop example
| Action | n | total |
|---|---|---|
| Start | — | 0 |
| First pass | 1 | 1 |
| Second pass | 2 | 3 |
| Third pass | 3 | 6 |
| After loop | — | 6 |
Trace for the condition example
| Input | Condition result | Branch | Output |
|---|---|---|---|
| mark = 50 | 50 > 50 is false | else | Try again |
Worked example
A loop stops before the intended final number
The loop body adds 1, 2, and 3. The range stop value, 4, is not included.
Python 3
total = 0
for n in range(1, 4):
total = total + n
print(total)- Set the intended resultAdding 1, 2, 3, and 4 gives an intended total of 10. Keep this target separate from the result of tracing the code.
- Trace each loop valueIn Python 3, range(1, 4) supplies 1, 2, and 3 because the stop value 4 is excluded. The total starts at 0 and changes once for each supplied value.
- Compare the resultThe traced total is 6, not 10. The mismatch begins with the range: it does not supply the intended final value, 4.
Worked example
A condition excludes a value that should qualify
The condition is false, so the No branch leads to the second output.
Python 3
mark = 50
if mark > 50:
print("Pass")
else:
print("Try again")- Record the input and ruleThe input is mark = 50. The intention includes 50, so the expected output is "Pass".
- Evaluate the written conditionThe code checks whether mark is greater than 50. Since mark is exactly 50, the condition is false and the else branch prints "Try again".
- Locate the mismatchThe traced output differs from the intended output at the comparison. The written condition requires a value strictly above 50, but the intention includes 50.
Worked example
A list update uses the wrong index
Python indexes start at 0. The code changes the value at index 2.
Python 3
readings = [6, 9, 12]
readings[2] = readings[2] + 1
print(readings)- Map positions to indexesPython list indexes start at 0. Here index 0 holds 6, index 1 holds 9, and index 2 holds 12. Therefore the second reading is at index 1.
- Apply the assignmentThe code changes readings[2], the third item, from 12 to 13. The other list values remain unchanged.
- Compare with the intentionThe traced list is [6, 9, 13], but the intended list is [6, 10, 12]. The assignment uses the index for the third item instead of the second.
Common mistakes and how to avoid them
Lesson summary
- Program state is the set of values currently stored in variables and data.
- To trace, state the intention and input, then follow each statement and record changes.
- Check loop values, condition results, and Python’s zero-based list indexes carefully.
- Compare the traced result with the expected result to find a mismatch.
Check your understanding
Question 1
- 2, 3, 4
- 2, 3, 4, 5
- 1, 2, 3, 4
- 3, 4, 5
Show answer and explanation
Question 2
- 8
- 11
- 14
- There is no item at index 1.
Show answer and explanation
Question 3
- The condition is true, so it prints "Ready".
- The condition is false, so the greater-than branch is skipped.
- The value of count changes to 4.
- The program must print both possible messages.
Show answer and explanation
Key terms
- Program state
- The values currently stored in a program’s variables and data.
- Trace
- A step-by-step record of how a program’s state and output change for a stated input.
- Index
- A number identifying an item’s position in a list; Python 3 list indexes start at 0.
- Intended behaviour
- A description of what a program is supposed to do for its input.
Continue through ICS3U
View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons
- A4.3 · Interpret programming-tool error messages
- A4.5 · Validate a program with varied test cases
- 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.4. 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.