2.2.17 Accumulators

Video: Accumulator Loops (7:26)
Build a Running Total of Soccer Goals

The Wildcats scored 2 goals in training round 1, no goals in round 2, and 3 goals in round 3. The application needs one total for those rounds.

An accumulator is a variable that keeps a running result. Each pass adds the current round's goals to the result already stored.

Read a Score and Carry the Total

Place both methods inside the existing Game class. GetRoundGoals supplies fixed practice data: it returns 2 for round 1, 3 for round 3, and 0 for other round numbers.

The method GetTrainingGoals uses those returned values to build its total.

Build a Running Total of Soccer Goals
public int GetRoundGoals(int round)
{
    if (round == 1)
    {
        return 2;
    }

    if (round == 3)
    {
        return 3;
    }

    return 0;
}

public int GetTrainingGoals()
{
    int totalGoals = 0;

    for (int round = 1; round <= 3; round++)
    {
        int roundGoals = this.GetRoundGoals(round);
        totalGoals = totalGoals + roundGoals;
    }

    return totalGoals;
}
Compare Current Score and Running Total
Running total after each round
Round Returned roundGoals Total before addition Total after addition
1 2 0 2
2 0 2 2
3 3 2 5
4; exit test No call 5 5; body skipped
Follow the Values and Execution Order

Start the running total before the loop

int totalGoals = 0; means no goals have been included yet. The initialization is outside the repeated body, so it happens once for this call.

The first pass can now add to a known starting value.

Read one score, then add it

On round 1, this.GetRoundGoals(round) returns 2. The body stores that result in roundGoals.

Next, totalGoals = totalGoals + roundGoals; calculates 0 + 2 and stores 2 back in the total.

Carry the earlier total into the next pass

Round 2 returns 0. Adding it leaves the total at 2. The zero-goal round is still visited and processed.

Round 3 returns 3. Its addition uses the earlier total of 2, producing 2 + 3, or 5.

Recognize the add-and-store pattern

You may see totalGoals += roundGoals; as a shorter form of this assignment. In this example it also adds the current score to the existing total.

The full form shows both the previous total and the new amount explicitly.

Distinguish the current amount from the running amount

roundGoals holds only the score returned for the current round. totalGoals includes that score and every score already processed. Before the third addition, those values are 3 and 2; after the addition, the running total is 5.

If you replace the total with the current score instead of adding, earlier rounds disappear from the result. Read the right side of the assignment carefully to see whether it preserves the previous total.

Read the Matching Activity Diagram
Build a Running Total of Soccer Goals.
Build a Running Total of Soccer Goals
  • New
  • Changed
  • Removed

This diagram follows GetTrainingGoals. The true route reads one score and adds it before advancing the round label. The loop returns to the condition without revisiting the zero initialization.

The return action is on the false route after traversal. It sends the complete total of 5 to the caller.

Try a Focused Change
Predict and Explain

Check your reasoning

Only the scores for rounds 1 and 2 are included: 2 and 0. The method returns 2 after two passes.

That equality is a coincidence. The body adds each round's goal amount, not one per pass. With all three original rounds, three passes produce five total goals.