2.2.13 Choosing Between for and while

Choose a Loop That Makes the Soccer Rule Clear

The Wildcats application repeats two small tasks: recording three rounds and reducing the number of players waiting at a station.

Both tasks need repetition. The best-looking loop form is the one that makes the continuation rule easiest to read.

Compare the Two Stopping Rules

A for header makes a planned range visible

The first task visits labels 1 through 3. The for header puts the start, boundary, and update together.

Each pass records one completion. When the next label is 4, the condition is false and the first task is finished.

A while condition can name the remaining work

The second task asks waitingPlayers > 0. As long as a player remains in the waiting count, another pass is needed.

Each body reduces the count by one. The counts before its tests are 2, 1, and 0; the test at zero ends repetition.

Preserve the order when changing forms

To rewrite the first loop as while, initialize round before the loop. Put the completion assignment and then the round update inside the body.

The condition must still be checked before each pass, and the update must still occur after that pass.

Choose from the meaning of the task

Ask how you would explain the next pass to a teammate. For the round task, the explanation is to move through round labels 1 to 3. For the station task, it is to continue while the waiting count is positive.

Both descriptions include a stopping rule. The loop keyword should bring the more useful description into view; it should not force a different meaning onto the task.

Predict Each Task
Two tasks and their separate exits
Task Successful test values First false test Final task result
Record three rounds round 1, 2, 3 round 4 completedRounds is 3
Process waiting count waitingPlayers 2, 1 waitingPlayers 0 waitingPlayers is 0
Read the Two Implementations

These are two independent statement groups inside a method. The second group starts after the first finishes. The waiting count is a simplified model of players completing a station.

Choose a Loop That Makes the Soccer Rule Clear
int completedRounds = 0;

for (int round = 1; round <= 3; round++)
{
    completedRounds = completedRounds + 1;
}

int waitingPlayers = 2;

while (waitingPlayers > 0)
{
    waitingPlayers = waitingPlayers - 1;
}
Read the Matching Activity Diagram
Choose a Loop That Makes the Soccer Rule Clear.
Choose a Loop That Makes the Soccer Rule Clear
  • New
  • Changed
  • Removed

The first repetition in the diagram corresponds to the for loop. Its false route leads to the initialization of the waiting count.

The second repetition corresponds to the while loop. It returns to its own condition, not to the round loop. The tasks happen one after the other, matching the source.

Try a Focused Change
Predict and Explain

Check your reasoning

Define int round = 1; before while (round <= 3). In the body, increase completedRounds and then execute round++;.

The body sees 1, 2, and 3, then the test at 4 is false. The count remains 3. Because round is now defined in the surrounding block, it remains in that block's scope after this while loop.