2.2.4 for Loops

Record Soccer Rounds with a for Loop

The Wildcats coach wants to record three completed passing rounds. A for loop lets the program repeat the same recording step without writing three copies of it.

The loop header brings together the starting value, the question that permits another pass, and the update for the next round.

Work Through the Soccer Example

This example is a group of statements to place inside an existing method. The counter models a recorded completion; increasing it does not itself make the team perform a physical drill.

Record Soccer Rounds with a for Loop
int completedRounds = 0;

for (int round = 1; round <= 3; round++)
{
    completedRounds = completedRounds + 1;
}
Follow the Values and Execution Order

Read the three parts of the header

In for (int round = 1; round <= 3; round++), the semicolons separate three jobs. First create the round label, then test it, then update it after a completed body.

The update round++ adds one to round. In this header it performs the same increment as round = round + 1.

These parts execute at different times. They are not three statements that all run before the body.

Initialize once, then ask the first question

int round = 1 creates the loop local and gives it the value 1. Initialization happens once. The program then evaluates round <= 3.

Because 1 <= 3 is true, execution enters the braces. The body changes completedRounds from 0 to 1.

Update after the body

After that body execution, round++ changes the label from 1 to 2. Execution returns to the condition.

The second true check permits the second completion, and the update then changes the label to 3. The third true check permits the third completion.

A final false check stops repetition

After the third completion, the update makes round equal to 4. The next comparison, 4 <= 3, is false.

The body does not execute for 4. There are three body executions and four condition checks, leaving completedRounds at 3.

Connect this to variable scope

completedRounds is defined before the loop, so the surrounding method can use it afterward. The header local round belongs to the loop and is out of scope after its closing brace.

Separate the round label from the recorded result

In this example the header advances round, while the body advances completedRounds. The label chooses which planned round is being processed; the result records how many completions the body has modeled.

Keeping those jobs separate makes the header easier to trace. At the final false test, the candidate label is 4 but the result is still 3. No fourth completion was recorded.

Trace This Example
Four visits to the condition
Round at test Condition Completed before body Completed after body or exit
1 True 0 1
2 True 1 2
3 True 2 3
4 False 3 3; body skipped
Read the Matching Activity Diagram
Record Soccer Rounds with a for Loop.
Record Soccer Rounds with a for Loop
  • New
  • Changed
  • Removed

The first two actions initialize the values. The true route records a completion and then advances the round label before returning to the diamond.

At 4, the false route goes to the final node. It bypasses both the completion assignment and the update.

Try a Focused Change
Predict and Explain

Check your reasoning

With an upper bound of 1, the body executes once and completedRounds finishes at 1. The update makes the next label 2, whose test is false.

With an upper bound of 0, the first comparison is 1 <= 0. It is false immediately, so neither the body nor the header update executes. The completed count stays 0.