Loop Control Statements
Read a for loop as a cycle rather than as a single line.
Why a For Loop Is a Cycle
A for loop is easy to misread. Written on one line, it looks like a single instruction that runs some work several times. The more useful way to read it is as a cycle: a repeated decision process that keeps coming back to the same checkpoints until it is told to stop.
What do you think happens?
In this loop, how many times does the body run? for (int i = 5; i < 5; i++) { System.out.println(i); }
Reveal answer
0 times
The loop starts by setting i to 5. Before any pass, the continuation condition i < 5 is checked. Since 5 is not less than 5, the condition is false, so the body never runs. The check happens before the body, not after.
Three Jobs in One Header
A for loop packages three jobs into one statement. It establishes an initial state, decides whether another repetition is allowed, and changes the state for the next repetition. Languages such as Java and C write all three jobs inside the parentheses of the loop header, separated by semicolons. The loop control variable is the variable these three jobs act on. In the examples below it is named i.
Initialization is the part that sets the loop control variable to its starting value. It runs once, before the first check.
The continuation condition is the test that decides whether another repetition is allowed. It is evaluated before each pass, and the body runs only while it is true.
The update is the change applied to the loop control variable so that the next check sees a new state. It runs after the body on each pass.
Tracing a For Loop One Pass at a Time
To understand what a loop does, follow its order of events. The source describes the sequence as: inspect the initialization, evaluate the continuation condition, run the body if the condition is true, and then perform the update. The condition is then evaluated again, and the cycle repeats until the condition is false.
Tracing a Loop That Prints Three Values
Trace the loop for (int i = 0; i < 3; i++) { System.out.println(i); } and determine what it prints.
- 1Initialization
The control variable i is set to 0. This happens once, before any check.
- 2First check
Is 0 less than 3? Yes, so the condition is true and the body runs.
- 3First pass body and update
The body prints 0. Then the update makes i equal 1.
- 4Second check and pass
Is 1 less than 3? Yes. The body prints 1, and the update makes i equal 2.
- 5Third check and pass
Is 2 less than 3? Yes. The body prints 2, and the update makes i equal 3.
- 6Final check
Is 3 less than 3? No, so the condition is false and control leaves the loop.
The loop prints 0, 1, and 2, then continues with the statement after the loop.
0
1
2
done- int i = 0 is the initialization, run once
- i < 3 is the continuation condition, checked before each pass
- i++ is the update, run after each pass
- The println inside the braces is the loop body
Reading the Control Variable Across Passes
The loop control variable is the thread that connects the passes. Each pass sees the value the previous update left behind. This is how the control variable directs repeated execution: the body does its work for whatever value i holds at that moment, and the condition decides whether another pass is allowed based on that same value.
| Moment | Value of i | Condition i < 3 | Body runs? | What happens next |
|---|---|---|---|---|
| After initialization | 0 | Not yet checked | No | Check the condition |
| First check | 0 | True | Yes, prints 0 | Update to 1 |
| Second check | 1 | True | Yes, prints 1 | Update to 2 |
| Third check | 2 | True | Yes, prints 2 | Update to 3 |
| Fourth check | 3 | False | No | Exit the loop |
Each row shows the state of the control variable at one checkpoint in the cycle.
When the Continuation Condition Never Fails
The source makes an important rule explicit: an empty continuation condition is treated as true. If the condition is left out of the header, nothing ever tests the loop control variable, so the check that would end the loop never fails. Control has no route out through the condition, and the loop keeps cycling.
- The middle part of the header is empty, so there is no continuation condition
- An empty condition is treated as true, so the body runs on every pass
- The update still changes i, but no check ever compares it to a limit
Notice the difference from the earlier example. The update still runs, so i keeps changing, but the cycle has no exit because the test that would stop it is missing. The question to ask is always where the exit path is, and whether the condition gives the cycle a point at which it can be false.
Finding Why a Loop Repeats Too Many or Too Few Times
When a loop's count is wrong, the source recommends tracking the control variable at the exact points where it is initialized, tested, and updated. Each symptom points to a different part of the header.
Common mistake The loop ends too soon
What it looks likefor (int i = 1; i < 1; i++) runs zero times, while the author expected one passWhy it failsThe initial value or the continuation condition stops the cycle before the intended work happens
Do this insteadInspect the initial value and the continuation condition first
Common mistake The loop repeats too many times
What it looks likefor (int i = 0; i <= 3; i++) runs four times when three were intendedWhy it failsA comparison that includes the limit, or an update direction that moves the wrong way, allows extra passes
Do this insteadInspect the condition and the direction of the update, then trace the last few checks
Common mistake The loop does not appear to finish
What it looks likefor (int i = 0; i < 3; ) { ... } with no update in the headerWhy it failsIf the update does not change the value the condition uses, the condition never becomes false
Do this insteadConfirm that the update actually changes the variable tested by the condition
Common mistake Leaving the continuation condition empty when a limit was intended, which makes the condition always true
Debug a loop by finding the first point where the expected control-flow route diverges. Write down the value of the control variable at initialization, at each test, and after each update, and compare that sequence with the number of passes you expected.
Practicing Loop Tracing
Trace this loop: for (int n = 10; n > 7; n--) { System.out.println(n); }. List the value of n at each check, state whether the body runs, and say what the loop prints. Then explain what would change if the update were n++ instead of n--.
Hints
- Start with the initialization: n is set to 10 once, before the first check
- Test n > 7 before each pass, and only run the body when the test is true
- With n--, the value falls by 1 after each pass, so ask when it stops being greater than 7
Key Takeaways
- A for loop combines three jobs in its header: initialization, a continuation condition checked before each pass, and an update applied after each pass.
- The loop control variable carries state from one pass to the next, and the condition uses that value to decide whether another repetition happens.
- Trace a loop by following the cycle of check, body, and update, and by recording the variable's value at each checkpoint.
- An empty continuation condition is treated as true, so no test ever ends the loop and it runs without a natural exit.
- When a loop repeats too many or too few times, inspect the initial value, the comparison operator, and the direction and effect of the update, in that order of priority for each symptom.