Skip to content

Trace a Program

Tracing means running code on paper: follow each line and write variable values in a table until the program stops.

Class 9 to 10beginner20 min3 lessons1 interactive lab
By the end you will be able to
  • Build a trace table with columns for each variable
  • Update values line by line without skipping
  • Find the line where output first goes wrong

Finish first

Lesson 1 of 3

Paper computer

When a program misbehaves, guessing is slow. Tracing is pretending you are the computer. You read one line, change variables, then move to the next line.

A trace table has a row per line and columns for each variable plus any output. Write the starting values before line one runs.

Open the trace table lab. Step through a short program and watch the table fill automatically. Then try predicting the next row before you click.

Interactive lab

Trace table

Step through a short program and watch variables update row by row. Predict the next state before you advance.

Trace the program

Variables on paper, line by line
LineCodexy
1x = 300
2y = 500
3x = x + y00
4y = x - 200
5print x00
1 / 6

Start

Tracing means running the program on paper. Write the value of each variable after every line. Computers do exactly this, just faster.

Continue

Lesson 2 of 3

Loops and choices in a table

When a loop runs, you may visit the same line many times. Add a new row each time that line executes, even if the line number repeats.

For an if statement, check the condition first. Only follow the branch that is true. Cross out the branch you skip so you do not drift.

The first row where reality diverges from your expectation is near the bug. Fix the plan there, not at the final wrong print.

Continue

Lesson 3 of 3

Tracing before coding

Experienced programmers still trace tricky parts on paper or in a debugger. The habit saves hours of random edits on homework and projects.

Start with tiny programs: ten lines, two variables. Master those before tracing nested loops in homework.

Next you will solve mazes by trying a path, backing up when blocked, and trying another. That is backtracking.

Continue

Practice

Work these out yourself

No answer key here on purpose: these are the questions worth thinking through before you move on. Open one and work it out.

0/3 attempted

Assessment

Check your understanding

Answer each question, then read the explanation. That is where the learning is.

0/4
  1. Question 1: What is tracing?
    Question 1 / 4

    What is tracing?

    Select an option first
  2. Question 2: A trace table usually includes what?
    Question 2 / 4

    A trace table usually includes what?

    Select an option first
  3. Question 3: When an if condition is false, you should what?
    Question 3 / 4

    When an if condition is false, you should what?

    Select an option first
  4. Question 4: The first wrong value in a table often points to what?
    Question 4 / 4

    The first wrong value in a table often points to what?

    Select an option first
4 of 4 questions left.

Finished this topic?

0 of 3 lessons marked done. Marking the topic complete ticks the rest.