🔍 Tracing

5th Grade 💻 Coding

Tracing means “running a program in your head”: you go through the code line by line, as if you were the computer, and write down what happens at each step.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

The most common way to do it is a trace table: one column for each variable and one row for each step. Every time a variable gets a new value, you write it in the table. That way you see exactly what the computer is “thinking” at every moment, and you spot where a value turns out different from what you expected.

Tracing is an important tool for learning, for computer science tests and for finding bugs. It forces you to read the code the way the computer reads it — without guessing “what the programmer meant.” Experienced programmers do a quick trace in their heads almost every time they write a loop.

For example

  • Tracing x = 2, x = x * 3, x = x + 1: after the first line x is 2, after the second it’s 6, and after the third it’s 7.
  • Jacob couldn’t figure out why his score kept going down; with a trace table he saw that one line had a minus where it should have had a plus.

📌 Remember: Tracing = I am the computer: line, value, line, value.

Quick quiz

  1. What is tracing?

  2. What do you write in a trace table?

  3. If x = 2 and then x = x * 3, what is x?

🔎 Ask Thinkapedia another question