2.2.7 Loop Initialization, Condition, and Update

Find the Start, Test, and Update of a Training Loop

The Wildcats plan three passing rounds. To explain how a loop reaches that plan, find its starting state, its continuation question, and the change that makes progress. These three parts work together.

Explore the Soccer Example

Place these statements inside an existing method. The while condition asks whether the recorded count is still below the plan.

Find the Start, Test, and Update of a Training Loop
int completedRounds = 0;
int plannedRounds = 3;

while (completedRounds < plannedRounds)
{
    completedRounds = completedRounds + 1;
}
Understand the Code

Initialize before asking the question

The assignments before the loop establish zero recorded completions and a plan of three. They run once. Starting the completed count at 1 would mean one round was already recorded, leaving only two additional passes to reach the same plan.

Read the condition as permission to continue

completedRounds < plannedRounds asks whether more completions are needed. At 0, 1, and 2, the answer is true. Each true check permits one body execution.

At 3, the answer is false. The body is skipped and execution continues after the loop.

Change the state used by the next test

The assignment inside the braces adds one to the completed count. The next test reads that new value. Without this update, a true condition would keep seeing the same count.

Predict the effect of changing one part

Keep the plan at 3 but change the starting completed count to 1. The loop should record two additional completions. Now restore zero and change the update to add two: the count would jump from 0 to 2 to 4.

Those are different behaviors even though both versions eventually fail the comparison. A loop must not merely stop; its start, test, and update must also perform the work the coach requested.

Check the Values
Worked soccer example
Count before test Condition Count after body or exit
0 True 1
1 True 2
2 True 3
3 False 3; no update
Connect the Picture to the Code
Find the Start, Test, and Update of a Training Loop.
Find the Start, Test, and Update of a Training Loop
  • New
  • Changed
  • Removed

The two starting assignments precede the decision. The true path updates completedRounds and returns directly to that decision. The false path bypasses the update. The return path never resets the starting values.

Try a Small Change
Predict the Result

Start completedRounds at 3 with the plan still 3. Which parts execute, and how many body passes occur?

Check your reasoning

The starting assignments execute, then the first comparison is false. The update does not execute, there are zero body passes, and the completed count remains 3.