Tracing Loops
in Java
Learn to step through loop execution line by line, track variable changes, and predict program output with confidence.
What Is Tracing?
Tracing means simulating the computer: you follow code line by line, recording how each variable changes at every step. Instead of guessing what a program does, you become the runtime and execute it on paper.
The Three Java Loops
Anatomy of a for Loop
System.out.print(i + " ");
}
Loop Execution Flow
for vs while vs do-while
| for | while | do-while | |
|---|---|---|---|
| Condition check | Before body | Before body | After body |
| Minimum runs | 0 | 0 | 1 |
| Update location | In header | Inside body | Inside body |
| Best when | Count known | Count unknown | Run at least once |
How to Build a Trace Table
i++). Record the new value and go back to step 3.Example 1: Simple Counter
for (int i = 1; i <= 4; i++) {
sum += i;
}
System.out.println(sum);
| Iteration | i | Condition \(i \le 4\) | sum (after update) |
|---|---|---|---|
| (init) | - | - | 0 |
| 1 | 1 | true | 1 |
| 2 | 2 | true | 3 |
| 3 | 3 | true | 6 |
| 4 | 4 | true | 10 |
| (exit) | 5 | false | 10 |
Example 2: while Loop Tracing
int count = 0;
while (n > 1) {
n /= 2;
count++;
}
System.out.println(count);
| Iteration | n | \(n > 1\) | count |
|---|---|---|---|
| Init | 64 | true | 0 |
| 1 | 32 | true | 1 |
| 2 | 16 | true | 2 |
| 3 | 8 | true | 3 |
| 4 | 4 | true | 4 |
| 5 | 2 | true | 5 |
| (exit) | 1 | false | 6 |
Example 3: Nested Loops
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
result += "(" + i + "," + j + ")";
}
}
| i | j | Output appended |
|---|---|---|
| 1 | 1 | (1,1) |
| 1 | 2 | (1,2) |
| 2 | 1 | (2,1) |
| 2 | 2 | (2,2) |
| 3 | 1 | (3,1) |
| 3 | 2 | (3,2) |
The Off-by-One Problem
The most common loop bug: your loop runs one too many or one too few times because of incorrect boundary conditions.
for (int i = 0; i < 5; i++)
// Prints 0 1 2 3 4 5 (6 values)
for (int i = 0; i <= 5; i++)
Tracing Accumulators
An accumulator is a variable that builds up a result across iterations: a running sum, product, or concatenated string. Tracing accumulators requires tracking the value before and after each operation.
for (int i = 1; i <= 5; i++) {
product *= i;
}
// product = 1*1*2*3*4*5 = 120 (5!)
| i | product (before) | product (after) |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 6 |
| 4 | 6 | 24 |
| 5 | 24 | 120 |
Tracing String Building
String rev = "";
for (int i = s.length() - 1; i >= 0; i--) {
rev += s.charAt(i);
}
| i | charAt(i) | rev |
|---|---|---|
| 4 | E | "E" |
| 3 | D | "ED" |
| 2 | C | "EDC" |
| 1 | B | "EDCB" |
| 0 | A | "EDCBA" |
Spotting Infinite Loops
An infinite loop occurs when the condition never becomes false. Tracing 2-3 iterations will reveal if the variable isn't moving toward termination.
int n = 10;
while (n > 0) {
System.out.println(n);
// Missing: n--;
}
Common Mistakes
i++ in a while loop causes an infinite loop. The for loop header makes this harder to forget.Tracing Cheat Sheet
| Concept | What to Check | Key Question |
|---|---|---|
| Initialization | Starting values of all variables | Sum starts at 0? Product at 1? |
| Condition | \(<\) vs \(\le\) boundary | Does the last iteration include the boundary? |
| Body | What changes each iteration | Which variables are modified? |
| Update | How the loop variable changes | Does it approach termination? |
| Termination | Final values after loop exits | What is the last value of \(i\)? |
Question 1 of 5
int x = 0; for (int i = 1; i <= 5; i++) { x += i * 2; } System.out.println(x);
What is the value of x after the loop finishes?
Question 2 of 5
int k = 100; int count = 0; while (k > 1) { k /= 10; count++; } System.out.println(count);
What does this code print?
Question 3 of 5
int n = 0; for (int i = 10; i > 0; i -= 3) { n++; } System.out.println(n);
How many times does the loop body execute?
Question 4 of 5
String s = ""; for (int i = 0; i < 4; i++) { if (i % 2 == 0) { s += "A"; } else { s += "B"; } } System.out.println(s);
What is printed?
Question 5 of 5
int a = 5; int b = 0; do { b += a; a--; } while (a > 2); System.out.println(a + " " + b);
What is the output of this do-while loop?