Unit 3 · Programming 2 · Lesson 3.4

The Accumulator

One running variable, updated a little on every pass, holding the answer at the end. It’s the pattern behind sum, count, max, and average — and behind half the hard questions on the exam. Learn it once; use it all year.

Big Idea 3 — Algorithms & ProgrammingThe melody of Unit 3

Before this lesson — read on Runestone

Watch for the “running total” examples — that’s exactly today. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs, and this is the lesson the whole unit leans on. You have loops (3.1–3.3). Put one variable inside a loop and update it each pass, and you can total, count, and track things across many steps. This pattern comes back in every unit that follows.

2

The Hook

How does a cashier total your cart? Not by memorizing every price at once. They keep one running number and add each item to it as it scans. Start at zero, add along the way, read the total at the end.

That running number is an accumulator, and the rhythm never changes: initialize it before the loop, update it inside, use it after. Master those three steps and you’ve got sum, count, max, min, and average — they’re all the same song in a different key.

3

The Idea

An accumulator is a variable that builds up a result across the passes of a loop. Every accumulator has the same three positions — and each has a common bug when you get it wrong:

StepWhereSum example
initializebefore the looptotal = 0
updateinside the loop bodytotal = total + x
useafter the loopprint(total)
Put “initialize” in the wrong place and everything breaks. Set total = 0 inside the loop and it resets to zero every pass, so you only ever keep the last item. Read the total inside the loop and you print a partial answer over and over. The three positions are not optional.

Python — sum a list

total = 0          # initialize
for x in [3, 1, 4]:
    total = total + x  # update
print(total)       # use → 8

AP Pseudocode

total ← 0
FOR EACH x IN [3, 1, 4]
{ total ← total + x }
DISPLAY(total)

Hover or tap a line to light its twin. Trace it: total starts 0, becomes 3, then 4, then 8. The answer, 8, exists only after the loop — inside, it’s always partial.

Same pattern, four jobs — only the initialize and update lines change: sum starts at 0 and adds x; count starts at 0 and adds 1 (often inside an if); max starts at the first value and keeps the larger; average is a sum divided by a count. One idea, four variations.
4

Try It — Watch It Accumulate

Step through a loop and watch the accumulator update on every pass. Switch between sum and count-evens to see the same pattern do two different jobs.

list:
5

Vocabulary

accumulator Programs
A variable that builds up a result across the passes of a loop — initialized before, updated inside, used after.
running total Programs
The everyday name for a sum accumulator: one number that grows as the loop adds to it.
6

Check

Five questions. Trace the accumulator pass by pass — write its value after each update — then pick your answer.

0 of 5 answered
7

Impact Check

One little variable, running the numbers behind everything

Your step count, your bank balance, a video’s view total, a game’s high score, the “items in cart” badge — every one is an accumulator quietly updating as events happen. When you see a number that grows over time, there’s almost always a loop somewhere adding to a running variable.

It scales all the way up: totaling a spreadsheet column, averaging millions of survey responses, counting votes. The idea doesn’t change from three numbers to three billion — only how many times the loop runs. That’s why this humble pattern is the melody of the whole course.

8

Connections

Came from 3.1–3.3 — the accumulator lives inside a loop; the loop just provides the passes.
Next in 3.5 — Loop Day, where a savings-until-goal program is an accumulator with a while loop.
Returns in 3.9 — max, min, average, and count-if are all named accumulator variations, straight from the CED.
Returns everywhere after — Unit 4 simulations, Unit 5 data analysis, and most Create PT programs are accumulators in disguise.
← 3.3 Counting Loops