cadence_learn
think_like_a_programmer / level 04 of 12 ▶ 15 min

be the computer.

Values are invisible, which is why broken programs feel like magic. Write every value down after every line and the magic turns into a table — with the bug sitting in one row of it.

after this level you'll be able to
  • Build a trace table for a short program without running it
  • Predict what a loop will produce before you execute it
  • Find the exact line where a program stops doing what you expected
the whole idea

be the computer, on paper

the one sentence

The single most useful thing you can do when a program is wrong is to stop reading it and start being it — walk the steps by hand, one at a time, writing down what every value is after every line.

what tracing actually does
THE PROGRAM (INVISIBLE VALUES) 1 total = 0 2 count = 1 3 while count ≤ 5: 4 total = total + count 5 count = count + 1 6 show total BY HAND THE TRACE TABLE (VALUES MADE VISIBLE) STEP LINE TOTAL COUNT 110 4411 7432 10463 134104 164155 ▼ WHY IT WORKS reading code shows you what you MEANT · writing the numbers down shows you what it DOES · the first row that surprises you is the bug
Values are invisible, and that is the whole problem. A program is text that does not move, while the numbers change somewhere you cannot see. The table drags them into the open — and then the bug has nowhere left to hide.

This is called tracing or a trace table, and it is the closest thing this subject has to a superpower. It is also, not coincidentally, a thing you will be asked to do on an exam with no computer in front of you.

Here is why it works. When you read code, your brain reads what you meant. It skims. It fills gaps — exactly the gaps from level one. When you trace, you cannot skim, because you have to write down an actual number after every single line. The line where your written number stops matching what you expected is the bug. Not near the bug. The bug.

the thing nobody tells beginners

Values are invisible. When a program misbehaves you are staring at text that does not move, trying to imagine numbers that are not written anywhere. Of course it feels impossible. Tracing makes the invisible thing visible, and the mystery mostly evaporates.

try it yourself

step through it one line at a time

Before you press anything: read the six lines and write down what you think the answer will be. Put your guess in the box. Being wrong here is genuinely useful — it shows you exactly which line you misread.

▤ lab 04 · the trace table

Press step. A row appears for every line that runs. Step back if you lose the thread.

1set total to 0
2set count to 1
3while count is 5 or less:
4 add count to total
5 add 1 to count
6show total
steplinetotalcount
Nothing has run yet. Write your guess down first, then press step.
◈ ask an ai about this

“What is a trace table and how do I make one? Walk me through a tiny example line by line.”

chatgpt ↗ claude ↗

what you just watched

the loop only ends because one line changes

Watch the count column. It went 1, 2, 3, 4, 5, 6 — and the moment it hit 6, the answer to “is count 5 or less?” became no, and the program left the loop. Line 5 is the only reason this program ever finishes.

Now watch total: 0, 1, 3, 6, 10, 15. It never jumps. Every value is the one before it plus the current count. If you had traced a broken version, the first row where that pattern breaks would be the line to go and look at. That is debugging, and you already know how to do it.

do this on paper in the exam

Four columns, one row per line that runs. It looks slow. It is slow. It is also the difference between an answer and a guess, and graders give marks for the table itself — your course description lists “test/debug” as a named skill, and this is what that means before you own a debugger.

◈ ask an ai about this

“How do I use tracing to find a bug? What exactly am I looking for as I fill in the table?”

chatgpt ↗ claude ↗

do this together

trace out loud

☶ two people · 12 minutes · one pen, one sheet

One reads the lines. One keeps the numbers. Neither of you gets to skim.

person a — the reader

Read one line out loud. Only one. Then stop and wait. Do not read ahead, and do not explain what is about to happen.

person b — the memory

After every line, say every value out loud and write it down. “total is 3, count is 3.” Every line. Even the boring ones.

Then change line 3 to “while count is 10 or less” and do it again — but predict the answer first. When your prediction and your table disagree, the table is right. That sentence will save you hours this semester.

what to keep

three things worth remembering

01

Stop reading, start tracing

Reading code shows you what you meant. Tracing shows you what it does.

02

The first surprising row is the bug

Not near it. It. Debugging is mostly finding the first row that surprises you.

03

Predict, then check

Guessing first turns a boring exercise into a test of what you actually believe.

check yourself

3 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself3 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

total starts at 0, count at 1. The loop adds count to total and then adds 1 to count, while count is 5 or less. What is total at the end?

set total to 0 set count to 1 while count is 5 or less: add count to total add 1 to count
02

And what is count when the loop finally stops?

03

You trace a broken program and row 7 is the first number that surprises you. Where is the bug?

answered: 0 of 3right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →