Concepts / Loop Control Statements

Loop Control Statements

Read a for loop as a cycle rather than as a single line.

  • Programming

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); }

  • 0 times

  • 1 time

  • 5 times

  • Forever

Reveal answer
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.

int i = 0Initialization: runs once,before the first passi < 3Continuation condition:checked before every passi++Update: runs after everypass
Which part of the for statement runs once, which is checked before each pass, and which runs after each pass?
Definition

Initialization is the part that sets the loop control variable to its starting value. It runs once, before the first check.

Definition

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.

Definition

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.

Initializationi = 0, runs onceContinuation checki < 3 ?Loop bodyprint iUpdatei = i + 1Exit loopcontinue with the nextstatementthen checktrueafter bodycheck againfalse
After the body runs, what happens next, and when does the loop stop or start another pass?
Worked example

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.

  1. 1
    Initialization

    The control variable i is set to 0. This happens once, before any check.

  2. 2
    First check

    Is 0 less than 3? Yes, so the condition is true and the body runs.

  3. 3
    First pass body and update

    The body prints 0. Then the update makes i equal 1.

  4. 4
    Second check and pass

    Is 1 less than 3? Yes. The body prints 1, and the update makes i equal 2.

  5. 5
    Third check and pass

    Is 2 less than 3? Yes. The body prints 2, and the update makes i equal 3.

  6. 6
    Final check

    Is 3 less than 3? No, so the condition is false and control leaves the loop.

Result

The loop prints 0, 1, and 2, then continues with the statement after the loop.

java
Output
0
1
2
done
Notes on the code
  • 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.

Pass 1i = 0, body prints 0Pass 2i = 1, body prints 1Pass 3i = 2, body prints 2Final checki = 3, condition failsupdateupdateupdate
What value does the loop control variable hold on each iteration, and how does that value decide what the body does?
MomentValue of iCondition i < 3Body runs?What happens next
After initialization0Not yet checkedNoCheck the condition
First check0TrueYes, prints 0Update to 1
Second check1TrueYes, prints 1Update to 2
Third check2TrueYes, prints 2Update to 3
Fourth check3FalseNoExit 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.

java
Notes on the code
  • 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 like
    for (int i = 1; i < 1; i++) runs zero times, while the author expected one pass
    Why it fails

    The initial value or the continuation condition stops the cycle before the intended work happens

    Do this instead

    Inspect the initial value and the continuation condition first

  • Common mistake

    The loop repeats too many times

    What it looks like
    for (int i = 0; i <= 3; i++) runs four times when three were intended
    Why it fails

    A comparison that includes the limit, or an update direction that moves the wrong way, allows extra passes

    Do this instead

    Inspect 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 like
    for (int i = 0; i < 3; ) { ... } with no update in the header
    Why it fails

    If the update does not change the value the condition uses, the condition never becomes false

    Do this instead

    Confirm 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

Best practice

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

MEDIUM

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.