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
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;
| Iteration | i | Condition | sum |
|---|---|---|---|
| Init | 1 | 0 | |
| 1 | 1 | \(1 \le 4\) ✓ | 0 + 1 = 1 |
| 2 | 2 | \(2 \le 4\) ✓ | 1 + 2 = 3 |
| 3 | 3 | \(3 \le 4\) ✓ | 3 + 3 = 6 |
| 4 | 4 | \(4 \le 4\) ✓ | 6 + 4 = 10 |
| Exit | 5 | \(5 \le 4\) ✕ | 10 |
Example 2: while with Accumulator
int n = 5, result = 1;
while (n > 1) {
result *= n;
n--;
}
cout << result;
| Iteration | n | Condition | result |
|---|---|---|---|
| Init | 5 | 1 | |
| 1 | 5 → 4 | \(5 > 1\) ✓ | 1 × 5 = 5 |
| 2 | 4 → 3 | \(4 > 1\) ✓ | 5 × 4 = 20 |
| 3 | 3 → 2 | \(3 > 1\) ✓ | 20 × 3 = 60 |
| 4 | 2 → 1 | \(2 > 1\) ✓ | 60 × 2 = 120 |
| Exit | 1 | \(1 > 1\) ✕ | 120 |
Example 3: Nested Loops
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << "\n";
}
| i | j runs | Output |
|---|---|---|
| 1 | j: 1 | * |
| 2 | j: 1, 2 | ** |
| 3 | j: 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 |
|---|---|---|
| 1 | false | 0 |
| 2 | true | 1 |
| 3 | false | 1 |
| 4 | true | 2 |
| 5 | false | 2 |
| 6 | true | 3 |
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;
| Iteration | x before | x / 2 | \(x < 10\)? |
|---|---|---|---|
| 1 | 100 | 50 | no |
| 2 | 50 | 25 | no |
| 3 | 25 | 12 | no |
| 4 | 12 | 6 | yes → 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
int x = 0;
for (int i = 1; i <= 5; i++) {
x += i * 2;
}
cout << x;
▼ Tap to read code
What is the output of this program?
Each iteration adds \(i \times 2\): \(2 + 4 + 6 + 8 + 10 = 30\). Build a trace table to verify each step.
Question 2 of 5
int n = 1;
while (n < 20) {
n *= 3;
}
cout << n;
▼ Tap to read code
What value of n is printed?
Trace: n = 1 → 3 → 9 → 27. At n = 27, the condition \(27 < 20\) is false, so the loop exits and prints 27.
Question 3 of 5
int val = 10;
do {
val -= 3;
} while (val > 0);
cout << val;
▼ Tap to read code
What does this do-while loop print?
Trace: 10 → 7 → 4 → 1 → -2. At val = 1, the body runs one more time: \(1 - 3 = -2\). Now \(-2 > 0\) is false, so the loop exits and prints -2.
Question 4 of 5
int s = 0;
for (int i = 10; i >= 1; i -= 3) {
s += i;
}
cout << s;
▼ Tap to read code
What is the output? Pay close attention to the loop bounds and step size.
i takes values: 10, 7, 4, 1. Sum: \(10 + 7 + 4 + 1 = 22\). Next i would be -2, which fails \(-2 \ge 1\).
Question 5 of 5
int c = 0;
for (int i = 0; i < 8; i++) {
if (i % 3 == 0) continue;
c++;
}
cout << c;
▼ Tap to read code
How many times does c get incremented?
i values 0-7: continue skips when \(i \% 3 == 0\), which is i = 0, 3, 6 (3 skips). That leaves \(8 - 3 = 5\) increments.