2.2.20 Common Array Traversal and Update Patterns

Choose the Work to Perform During Array Traversal

The same traversal header can read, count, total, or update array elements. What changes is the body. Choose the body to match the coach's question instead of treating every loop as the same calculation.

Explore the Soccer Example

The coach wants a total of all goals and a count of rounds that scored at least two. The total preserves earlier scores, while the event count changes only when the current score qualifies.

Choose the Work to Perform During Array Traversal
int[] roundGoals = { 2, 0, 3 };
int totalGoals = 0;
int highScoringRounds = 0;

for (int index = 0; index < roundGoals.Length; index++)
{
    int goals = roundGoals[index];
    totalGoals = totalGoals + goals;

    if (goals >= 2)
    {
        highScoringRounds = highScoringRounds + 1;
    }
}
Understand the Code

Visit every element with one consistent header

The loop visits indexes 0, 1, and 2. The local goals receives the current element value on each pass.

The boundary and update determine which positions are visited; the body determines what summary is built from their values.

Add an amount to produce a total

The totalGoals assignment adds the complete score each time. Its running values become 2, 2, and 5. It answers how many goals were recorded across all rounds.

Add one only when a condition is met

The highScoringRounds assignment runs only for scores at least 2. Scores 2 and 3 qualify; score 0 does not. The final event count is 2.

Do not add the score itself to this counter, because the question asks how many qualifying rounds, not their goal total.

Know whether the source array changes

This body reads elements into a local and changes summary variables. It never assigns into roundGoals[index], so the source scores remain 2, 0, 3.

An element-update pattern instead places the indexed element on the left side of an assignment.

Choose a summary before choosing its update

A total adds a magnitude: this round's number of goals. A qualifying-round count adds one: this round either meets the rule or does not.

The same element can contribute differently to both summaries. Score 3 adds three goals to totalGoals but only one qualifying round to highScoringRounds. Read the question before deciding what to add.

Try a Small Change
Predict the Result
Connect the Picture to the Code
Choose the Work to Perform During Array Traversal.
Choose the Work to Perform During Array Traversal
  • New
  • Changed
  • Removed

The total assignment is on every true loop route. The event-counter assignment is inside a second decision and can be skipped. Both routes from that decision rejoin before index++ so a nonqualifying score still advances traversal.

Review Both Summaries Together

The following table reviews the original goals >= 2 condition. The stricter practice condition changes only the qualifying-round count.

Worked soccer example
Index Score read Total after pass High-scoring count after pass
0 2 2 1
1 0 2 1
2 3 5 2
3; exit No read 5 2