Accumulator in AP Computer Science A

In AP Computer Science A, an accumulator is a variable you initialize before a loop and update on every iteration to build up a result, like a running sum, product, count, or String, so the final value is ready when the loop ends.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is the accumulator?

An accumulator is a variable that collects a result piece by piece as a loop runs. You set it up before the loop (sum = 0, product = 1, or result = ""), then inside the loop you update it every iteration, usually with something like sum += value or result += nextPart. When the loop finishes, the accumulator holds the answer.

The pattern always has three moves. First, initialize the accumulator to the identity value for your operation (0 for addition, 1 for multiplication, the empty string for concatenation). Second, update it inside the loop body each time through. Third, use the final value after the loop. The accumulator is NOT the loop control variable from EK 2.8.A.2. The loop control variable (usually i) decides how many times the loop runs; the accumulator stores what the loop is actually computing.

Why the accumulator matters in AP® Computer Science A

Accumulators live in Topic 2.8 (For Loops) in Unit 2: Selection and Iteration, supporting learning objective 2.8.A: develop code to represent iterative processes using for loops and determine the result of those processes. "Determine the result" is the key phrase. Almost every loop-tracing question on the exam is really asking you to track an accumulator's value iteration by iteration. And almost every FRQ that says "return the sum," "return the average," or "build a String" is asking you to write the accumulator pattern correctly. If you can initialize, update, and return an accumulator without bugs, you've unlocked a huge share of the methods AP CSA asks you to write.

How the accumulator connects across the course

Counter Variable (Unit 2)

A counter is just an accumulator that always adds 1. You use a counter to count how many times something happens, and a general accumulator to add up the actual values. Both follow the same initialize-then-update pattern.

For Loops and Loop Control Variables (Unit 2)

Per EK 2.8.A.2, the loop control variable is initialized in the for loop header and controls how many iterations happen. The accumulator is a separate variable declared before the loop. Mixing them up, or declaring the accumulator inside the loop body so it resets every iteration, is one of the most common bugs on FRQs.

String Concatenation (Unit 2)

Accumulators aren't just for numbers. Starting with String result = ""; and concatenating inside a loop builds a String one piece at a time. The 2019 FRQ on delimiters tested exactly this kind of String-building traversal.

Array and ArrayList Traversal (Unit 4)

The accumulator pattern scales straight into Unit 4. Summing an array, averaging an ArrayList, or finding a max all use a loop over the collection with an accumulator tracking the running result. Learn it in Unit 2 and you'll reuse it constantly.

Is the accumulator on the AP® Computer Science A exam?

On multiple choice, accumulators show up in code-tracing stems like "What is the value of sum after this code executes?" You need to track the accumulator's value through each iteration, often in a table. On free response, the accumulator pattern is a workhorse. The 2022 FRQ Q3 (the Review class) required summing integer ratings across a collection to compute results, and the 2019 FRQ Q3 required building a String by accumulating pieces between delimiters. Graders specifically check three things: did you initialize the accumulator to the right starting value, did you update it inside the loop (not outside), and did you return or use the final value after the loop ends. An accumulator initialized to the wrong value, or reset inside the loop, loses points even if the rest of the logic is right.

The accumulator vs counter variable

A counter is a specific kind of accumulator. A counter adds exactly 1 each time a condition is met, so it tells you HOW MANY (how many even numbers, how many passing scores). A general accumulator adds the value itself, so it tells you HOW MUCH (the total of all scores, the product of all factors). If the question asks "how many," write count++. If it asks for a total, average, or combined String, write sum += value or result += piece.

Key things to remember about the accumulator

  • An accumulator is initialized once before the loop and updated on every iteration to build a running result like a sum, product, count, or String.

  • Initialize the accumulator to the identity value for the operation, which means 0 for sums and counts, 1 for products, and the empty string for String building.

  • The accumulator is not the loop control variable. The loop control variable (like i) controls iterations, while the accumulator stores the result being computed.

  • Declaring or resetting the accumulator inside the loop body wipes it out every iteration, which is one of the most common point-losing bugs on FRQs.

  • A counter is just an accumulator that adds 1 each time a condition is true, so use a counter for 'how many' questions and a full accumulator for 'how much' questions.

  • To compute an average, accumulate the sum in the loop, then divide by the count after the loop ends.

Frequently asked questions about the accumulator

What is an accumulator in AP Computer Science A?

An accumulator is a variable you initialize before a loop and update each iteration to aggregate a result, like sum += scores[i] building a running total. It's central to Topic 2.8 (For Loops) and learning objective 2.8.A.

Is the accumulator the same as the loop control variable?

No. The loop control variable (usually i) lives in the for loop header and controls how many times the loop runs, per EK 2.8.A.2. The accumulator is a separate variable declared before the loop that stores the result the loop is computing.

What's the difference between an accumulator and a counter?

A counter is a special case of an accumulator that adds exactly 1 each iteration to count occurrences. A general accumulator adds the actual values, so it produces a total or combined result instead of a tally.

What value should I initialize an accumulator to?

Use the identity value for your operation. Start sums and counts at 0, products at 1, and String accumulators at the empty string "". Starting a product at 0 means the result stays 0 forever, a classic trap.

Do accumulators show up on AP CSA FRQs?

Yes, constantly. The 2022 FRQ Q3 required accumulating rating values from Review objects, and the 2019 FRQ Q3 required accumulating pieces of a String between delimiters. Any FRQ asking for a sum, average, count, or built-up String is an accumulator problem.