01 / 21

Tracing Loops
in C++

Learn to step through loop execution line by line, track variable changes, and predict output with confidence.

C++ High School CS

What Is Loop Tracing?

Loop tracing means manually stepping through each iteration of a loop, recording how variables change at every step, and determining the final output.

Think of yourself as the computer: you read one line at a time, update values in your head (or on paper), and check the condition before each pass.

Anatomy of a for Loop

for (int i = 0; i < 5; i++) { cout << i << " "; }
Initialization
Runs once before the loop starts: sets the counter variable.
Condition
Checked before each iteration: loop continues while true.
Update
Executes after the body on each pass: advances the counter.

Why Tracing Matters

1
Debugging: You can identify exactly which iteration causes a bug.
2
Exam preparation: AP CS and university exams frequently ask you to predict loop output.
3
Algorithm understanding: Complex algorithms are just loops with patterns you can trace.
4
Off-by-one prevention: Tracing reveals whether you iterate too many or too few times.

The Loop Execution Cycle

Initialize Condition true? true Execute Body Update false End

for vs while vs do-while

Feature for while do-while
Init location In header Before loop Before loop
Condition check Before body Before body After body
Min iterations 0 0 1
Best for Known count Unknown count Run-then-check

How to Trace Step by Step

1
Draw a trace table. Create one column for each variable and one for the output.
2
Write the initial values. Record the starting value of each variable after initialization.
3
Evaluate the condition. If true, proceed to the body. If false, stop.
4
Execute the body. Update any variables or output in your table row.
5
Apply the update expression. Then go back to step 3 for the next iteration.

Example 1: Simple for Loop

int sum = 0; for (int i = 1; i <= 4; i++) { sum += i; } cout << sum;
IterationiConditionsum
Init10
11\(1 \le 4\) ✓0 + 1 = 1
22\(2 \le 4\) ✓1 + 2 = 3
33\(3 \le 4\) ✓3 + 3 = 6
44\(4 \le 4\) ✓6 + 4 = 10
Exit5\(5 \le 4\) ✕10

Example 2: while with Accumulator

int n = 5, result = 1; while (n > 1) { result *= n; n--; } cout << result;
IterationnConditionresult
Init51
15 → 4\(5 > 1\) ✓1 × 5 = 5
24 → 3\(4 > 1\) ✓5 × 4 = 20
33 → 2\(3 > 1\) ✓20 × 3 = 60
42 → 1\(2 > 1\) ✓60 × 2 = 120
Exit1\(1 > 1\) ✕120

Example 3: Nested Loops

for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { cout << "*"; } cout << "\n"; }
ij runsOutput
1j: 1*
2j: 1, 2**
3j: 1, 2, 3***

Loops with Conditionals

int count = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) count++; } cout << count;
i\(i \% 2 == 0\)?count
1false0
2true1
3false1
4true2
5false2
6true3

break and continue

break
Immediately exits the entire loop. Execution jumps to the first statement after the closing brace.
continue
Skips the rest of the current iteration and jumps to the update expression (for) or condition check (while).
for (int i = 0; i < 10; i++) { if (i == 3) continue; // skip i=3 if (i == 6) break; // stop at i=6 cout << i << " "; } // Output: 0 1 2 4 5

Tracing a break Statement

int x = 100; while (true) { x /= 2; if (x < 10) break; } cout << x;
Iterationx beforex / 2\(x < 10\)?
110050no
25025no
32512no
4126yes → break

Off-by-One Errors Explained

The most common loop bug: your loop runs one too many or one too few times. Tracing catches these instantly.

// Version A: prints 0 1 2 3 4 (5 values) for (int i = 0; i < 5; i++) // Version B: prints 0 1 2 3 4 5 (6 values) for (int i = 0; i <= 5; i++)

Common Mistakes

✕ Forgetting the Update
Omitting i++ in a while loop creates an infinite loop: the condition never changes.
✕ Wrong Initial Value
Starting at 1 instead of 0 (or vice versa) shifts every iteration by one, changing the final output.
✕ Confusing \(<\) with \(<=\)
Using \(<=\) when you meant \(<\) adds an extra iteration. Always trace the boundary value.

Tracing Cheat Sheet

Concept What to Check Tip
for loop Init, condition, update All three in the header
while loop Condition before body Update must be in body
do-while Body runs first Min 1 iteration
Nested loops Inner resets each outer pass Trace inner loop fully for each outer pass
break Exits loop immediately Check after-loop variable
continue Skips to next iteration Update still runs (for)

Question 1 of 5

What is the output of this program?

Question 2 of 5

What value of n is printed?

Question 3 of 5

What does this do-while loop print?

Question 4 of 5

What is the output? Pay close attention to the loop bounds and step size.

Question 5 of 5

How many times does c get incremented?