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.

A program’s state is the collection of values currently stored in its variables and data. Tracing means following those values through the statements in order. It helps you notice where the program’s behaviour first differs from what it is supposed to do. You can trace code by reading it carefully; the examples below derive their results without running the code. They use Python 3. Python list indexes start at zero, so the first item is at index 0. Other classroom languages may use different syntax or indexing rules.

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

Begin by describing what the program is meant to do and identifying its input. Record the starting values of the variables. Then read each statement in order, updating only the values that statement changes. For a loop, note the value used on each pass and when the loop stops. For a decision, evaluate its condition using the current values before choosing a path.
A trace table can record each important change: the action, relevant variable or list values, and any output. Keep the record focused on the values that affect the result. At the end, compare the result you traced with the intended result.
  • 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

First, state the intended behaviour in ordinary language. Choose a small input whose expected result you can work out. Trace the code as written, without silently correcting it. If a loop or condition is involved, check the exact values it uses and the comparisons it makes. Look for the first point where the state or output conflicts with the intention; that is a useful place to investigate.
A trace tells you what the written statements do for the input you chose. It does not establish what happens for every possible input. If a result seems surprising, check that you copied the input correctly, followed the statements in order, and applied the language’s rules accurately. In Python 3, the stop value in range is excluded, and list indexes begin at zero.
  • 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

When reviewing code, ask what values are stored before each statement, what that statement changes, which condition is being checked, and what output has appeared so far. These questions turn a vague suspicion into reasoning that someone else can follow.
Small inputs make state changes easier to inspect. If a program is meant to count items, use a short list and count its items yourself. If it adds a range of numbers, write down the values the loop actually receives. If it changes a list, record the relevant indexes and values. Keep the intended result separate from the result you derive from the code.
  • 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

Actionntotal
Start—0
First pass11
Second pass23
Third pass36
After loop—6

Trace for the condition example

InputCondition resultBranchOutput
mark = 5050 > 50 is falseelseTry again

Worked example

A loop stops before the intended final number

Intention: add every whole number from 1 through 4 and print the total. Input: none; the values come from the loop. Trace the code and decide whether it matches the intention.
Values in the loop
each valuenextfinishedtotal = 0n = 1, then 2, then 3total = total + nPrint total

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)
  1. Set the intended result
    Adding 1, 2, 3, and 4 gives an intended total of 10. Keep this target separate from the result of tracing the code.
  2. Trace each loop value
    In 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.
  3. Compare the result
    The traced total is 6, not 10. The mismatch begins with the range: it does not supply the intended final value, 4.
Answer: The trace gives an output of 6. For the stated intention, the Python range would need a stop value of 5 so that 4 is included.
Check: The loop receives 1, 2, and 3. Adding them to the starting total of 0 gives 6.

Worked example

A condition excludes a value that should qualify

Intention: print "Pass" for a mark of 50 or more, and "Try again" otherwise. Input: mark is 50. Trace the decision and check whether it matches the rule.
Decision for mark 50
YesNomark = 50mark > 50?Print PassPrint Try again

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")
  1. Record the input and rule
    The input is mark = 50. The intention includes 50, so the expected output is "Pass".
  2. Evaluate the written condition
    The code checks whether mark is greater than 50. Since mark is exactly 50, the condition is false and the else branch prints "Try again".
  3. Locate the mismatch
    The traced output differs from the intended output at the comparison. The written condition requires a value strictly above 50, but the intention includes 50.
Answer: For mark = 50, the code prints "Try again" according to the trace. The condition needs to include equality to match the stated rule.
Check: A value equal to 50 is not greater than 50, so the condition is false.

Worked example

A list update uses the wrong index

Intention: increase the second reading by 1, then print the updated list. Input list: [6, 9, 12]. Trace the list update using Python 3 indexing.
Python list before and after the update
Index → stored value0619212 → 13

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)
  1. Map positions to indexes
    Python 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.
  2. Apply the assignment
    The code changes readings[2], the third item, from 12 to 13. The other list values remain unchanged.
  3. Compare with the intention
    The 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.
Answer: The trace gives [6, 9, 13]. In Python, the second item is at index 1.
Check: Index 2 refers to the third item, so that is the value the code changes.

Common mistakes and how to avoid them

Changing the code mentally while tracing it.
Correction: Trace the statements exactly as written first. Then identify the statement that conflicts with the intention.
Assuming a Python loop includes its stop value.
Correction: In Python 3, range(start, stop) excludes stop. List the values the loop receives before updating the state.
Calling the first item in a Python list index 1.
Correction: Python indexes start at 0. Translate a position such as “second item” into its index before tracing an update.
Treating one trace as proof that the program works for every input.
Correction: A trace shows what happens for the stated input. Record that input and compare its result with the intention.

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

In Python 3, what values are supplied by range(2, 5)?
  1. 2, 3, 4
  2. 2, 3, 4, 5
  3. 1, 2, 3, 4
  4. 3, 4, 5
Show answer and explanation
2, 3, 4
The start value is included, but the stop value 5 is excluded.

Question 2

A Python list is [8, 11, 14]. Which value is at index 1?
  1. 8
  2. 11
  3. 14
  4. There is no item at index 1.
Show answer and explanation
11
Python indexes begin at 0, so index 1 identifies the second item, 11.

Question 3

The intention is to print "Ready" when count is at least 3. The code checks count > 3. What happens when count is 3?
  1. The condition is true, so it prints "Ready".
  2. The condition is false, so the greater-than branch is skipped.
  3. The value of count changes to 4.
  4. The program must print both possible messages.
Show answer and explanation
The condition is false, so the greater-than branch is skipped.
Three is not greater than three. The condition is false for this input, so the written comparison does not match the stated intention.

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

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.

Official curriculum reference

Report a correction or ask a question