04. Tracing a program by hand

There are two ways to find out what a program does. Run it and watch, or be the computer: read it one line at a time, in order, working out each line on paper. That second skill is tracing, the most useful thing in this part of the book. A programmer who can trace code can read it, predict it, and find the bug — without ever pressing run.

Being the computer, slowly

Back in Chapter 01 you learned that a computer does exactly what it is told, in order, top to bottom. Tracing is that same job by hand. Line by line. Never skip ahead. Never assume. When a line changes a value, cross out the old value and write the new one.

What makes this reliable is a trace table: a grid with one column per variable and one row for each change. You fill it in as you read.

A worked trace

Here is a short program. Do not run it. Read it line by line, keeping a table.

local a = 5
local b = 3
local total = a + b
a = a + 1
print(total)
print(a)

A local x = ... line makes a name and puts a value in it. A plain x = ... line changes a value that already exists. The table, one line at a time:

Line a b total
local a = 5 5
local b = 3 5 3
local total = a + b 5 3 8
a = a + 1 6 3 8

At print(total), the table says total is 8. At print(a), it says a is 6. So the output is:

8
6

The catch: total was worked out once, on the third line, while a was still 5. Changing a afterwards does not redo total. The table makes that obvious — total never changed after the row that set it. Tracing reveals this; guessing hides it.

Tracing a loop

A loop runs the same lines several times, so the table gets several rows for one variable. Trace this:

local n = 4
local result = 1
for i = 1, n do
    result = result * i
end
print(result)

The loop runs with i as 1, then 2, 3, 4. Each time, the body multiplies result by the current i:

Round (i) result before result after
1 1 1
2 1 2
3 2 6
4 6 24

After the loop, result is 24, so the program prints 24. (You just traced 1×2×3×4. That answer has a name — "4 factorial" — but you did not need to know that to trace it.)

Tracing a decision

For an if, you do not fill a table — you follow the one path whose question is true. Trace this with score of 72:

local score = 72
if score >= 90 then
    print("A")
elseif score >= 70 then
    print("B")
else
    print("C")
end

Read it aloud: is 72 at least 90? No, skip. Is 72 at least 70? Yes — print B, skip the rest. The output is B. Only the first true branch runs; the rest are never looked at.

On paper, change the first line of the loop example to local n = 5. Add one more row to the trace table and work out the new printed value. (You should get a number five times bigger than before.)

Why trace at all?

  • It finds bugs before you run. Most bugs are a line that does something slightly different from what you pictured. Tracing forces you to read what each line actually says.
  • It reads other people's code. Soon you will paste Roblox examples from the internet. Tracing is how you understand them instead of hoping.
  • It makes you slow down where it matters. A trace table is boring to fill in, and that is the point — boredom is your brain refusing to skip the hard line.

Homework

Paper again. No computer. For each problem, build a trace table (or follow the branch) and write down the final output. Only after you commit to an answer may you check it, once you reach the chapter that lets you run code.

Problem 1 — Sum of a countdown

Trace this program and write the printed value.

local s = 0
for i = 5, 1, -1 do
    s = s + i
end
print(s)

(Hint: the loop counts downi is 5, then 4, 3, 2, 1.)

Problem 2 — Building a string

Trace this and write exactly what is printed.

local word = "ha"
local out = ""
for i = 1, 3 do
    out = out .. word
end
print(out)

The .. glues two pieces of text together. out starts empty.

Problem 3 — The swap

Trace this with a two-column table for a and b. Write the final output line.

local a = 2
local b = 7
local temp = a
a = b
b = temp
print(a, b)

Explain in one sentence what job the temp variable is doing.

Challenge — Spot the surprise

Trace this and write the printed value. Most people guess wrong on the first read, so go slowly and trust the table.

local price = 100
local discount = 20
local final = price - discount
discount = 50
print(final)

Then write one sentence explaining why changing discount on the second-to-last line did or did not affect final.

Stuck or finished? Open the homework solutions page.