A girl and a robot thinking together

Think together · First, a short story

The answer on screen is wrong, so the robot suggests slowing down: after each statement, write down the variable values.

Lay out the process

Follow the numbered steps, then explain them in your own words.
  1. Startx = 2
  2. Add 3x = 5
  3. Multiply by 2x = 10
  4. Print10

Tracing a program means recording state changes in execution order.

Your table can include “current statement,” “before,” and “after.” Start with a small input so each step is easy to see.

01Why is “understanding the code” not enough?

Many learners read a code snippet and think, “I recognize every word,” but still cannot tell what it will print.

That is because code is not static text; it is executed dynamically according to control flow.

02The three most important things to track

Current statement

Where has execution reached?

Variable state

What is the current value of each important variable?

Output

What has been printed so far?

03Start with a small example

int sum = 0;

for (int i = 1; i <= 3; i++) {
    sum = sum + i;
}

std::cout << sum;

Do not just guess the result in your head. Trace each iteration.

04Draw a variable table

MomentisumOutput
Before loop—0empty
After iteration 111empty
After iteration 223empty
After iteration 336empty
After loop—66

A table makes the “execution in your head” visible and much easier to follow.

05How do you trace an if statement?

Evaluate the condition first, then choose exactly one branch.

if (x % 2 == 0) {
    std::cout << "even";
} else {
    std::cout << "odd";
}
Calculate x % 2→Check whether it equals 0→Choose a branch

06How do you trace a loop?

For every iteration, ask the same questions:

① Condition

Can it continue?

② Loop body

What changes this iteration?

③ Update

How does the control variable change?

④ Next iteration

Check the condition again.

07What about break and continue?

Do not keep reading along the original straight-line path.

break

Jumps immediately to the code after the loop.

continue

Ends the current iteration and proceeds according to that loop's rules.

08Is a program trace exactly like a real CPU?

No. We trace the execution semantics of the C++ source code.

A real CPU executes machine instructions after compilation and optimization. At the hardware level, mechanisms such as pipelining, out-of-order execution, and caches may also be involved.

What have you learned?

  • Trace control flow instead of judging code by its appearance.
  • Track the current statement, variable state, and output together.
  • For an if statement, evaluate the condition before choosing a branch.
  • For loops, track the condition, body, update, and next iteration each time.
  • break and continue immediately change where execution proceeds.
  • A source-level trace describes language semantics, not the CPU's instruction-by-instruction implementation.

Topic complete: The next topic is “Arrays and Strings.”

Your turn to try

If you swap the order of “add 3” and “multiply by 2,” is the result still 10?

Ready? Open the explanation

No. First multiplying gives 4, then adding gives 7. Tracing reveals that order matters, not just that the final answer changed.