Code Tracing
Flow of Execution: Code Tracing
Tracing the flow of execution through code, and telling how it will behave, is a key skill for reading code and debugging it. Here are four common methods:
Hand-tracing means stepping through the code line by line as if you were the computer, keeping track of the current value of every variable. On paper (or a whiteboard), write a row (or column) for each variable. Each time a value changes, cross off the old value and write the new one.
Temporarily adding
printcalls at various places shows what values your variables hold at those points. Delete them when you're done.Many programming environments include a debugger, which lets you step through the code line by line and watch how the variables change.
Explaining your code to someone else can give you an "Aha!" moment where you see the gap between what the code should do and what it does. It even works when you explain it to a pet or a rubber duck.
All of these are worth being comfortable with.
A worked trace
Here is a hand trace of a short program. Each row is one line of code, and each column is a variable. A row shows every variable's value after that line runs. A variable that has not been assigned yet is left blank.
count: int = 3
total: int = 10
count = count + 1
total = total - count
| After line | count | total |
|---|---|---|
count = 3 |
3 | |
total = 10 |
3 | 10 |
count = count + 1 |
4 | 10 |
total = total - count |
4 | 6 |
Work on the right side first, using the values the variables hold right now, then store the result in the variable on the left.
Now try the same idea with a swap. Trace this program, then set final_x and final_y.
Check your understanding. Trace the program one line at a time. Write a row for each line, as in the table above.
x: int = 3
y: int = 8
x = x + y
y = x - y
x = x - y
Predict what each variable holds after the program finishes. The program is not run for you. Replace each None in the box below with your prediction, and give each variable a type hint, like name: type = value. Then press Run to find out if you are right.
final_xis the value ofxat the endfinal_yis the value ofyat the end
Hint 1
After x = x + y, x holds 11. Use that new value on the next line.
Practice: predict the result
Each program below is shown but not run. Trace it by hand, then set the variables in the code cell to the values you expect at the end. Press Run to check. If a value is wrong, trace the program again one line at a time.
- Trace this program.
a: int = 4
b: int = a + 1
a = b * 2
b = a - b - 1
Set final_a and final_b to the values of a and b after the last line.
Hint 1
Write a row for each line and update the column of the variable that line changes.
- Trace this program. The loop body runs once for each value of
i.
total: int = 0
for i in range(1, 4):
total = total + i * 2
Set final_total to the value of total and final_i to the value of i after
the loop ends.
Hint 1
range(1, 4) gives 1, 2 and 3. Add a row to your table for each pass.
- Trace this program. Both
ifstatements are checked, one after the other.
score: int = 7
if score > 5:
score = score - 3
if score > 5:
score = score * 10
else:
score = score + 1
Set final_score to the value of score at the end.
Hint 1
The second if sees the value of score after the first one has changed it.
- Trace this program. The loop repeats until its condition is false, and the number of passes is part of the answer.
n: int = 10
steps: int = 0
while n > 1:
n = n // 2
steps = steps + 1
Set final_n to the value of n and final_steps to the value of steps after the loop ends.
Hint 1
Write a row for each pass: n becomes 5, then 2, then... Check the condition before every pass.