In AP Computer Science A, an accumulator variable is a variable initialized before a loop (often to 0 or 1) and then updated inside the loop body each iteration to build up a running result, like a sum, count, or product (Topic 2.8, Unit 2).
An accumulator variable is a variable you set up before a loop starts and then update inside the loop body on every pass. The classic pattern looks like this: int sum = 0; before the loop, then sum += value; inside it. By the time the loop ends, the accumulator holds the combined result of every iteration.
The two pieces of the pattern matter equally. The initialization gives the accumulator a sensible starting value (0 for sums and counts, 1 for products, or the first element when finding a max or min). The update statement folds in one new piece of data each time the loop body runs. This is different from the loop control variable in the for loop header, which exists to drive the loop itself (EK 2.8.A.2). The accumulator lives in the body and collects the answer, while the loop control variable just keeps the loop moving.
Accumulator variables live in Topic 2.8 (For Loops) in Unit 2: Selection and Iteration, supporting learning objective 2.8.A, which asks you to develop code for iterative processes using for loops and determine the result of those processes. That second half is where accumulators show up constantly. When an MCQ asks "what is the value of sum after this loop runs?", you're tracing an accumulator. The pattern also scales with you through the whole course. Almost every algorithm you write later (averaging an array, counting matches in an ArrayList, building a String) is just an accumulator wrapped in a traversal. If you can spot the init-then-update pattern early, half of AP CSA's loop questions become routine.
Keep studying AP® Computer Science A Unit 2
Loop control variable (Unit 2)
EK 2.8.A.2 defines the loop control variable as the one initialized in the for loop header to drive iteration. The accumulator is its partner in the loop body. One controls how many times the loop runs, the other collects the result of running it.
While loops (Unit 2)
The accumulator pattern isn't tied to for loops. A while loop summing user input until a sentinel value uses the exact same recipe, which is to initialize before the loop and update inside it. Recognizing the pattern works regardless of which loop wraps it.
Counter variable (Unit 2)
A counter is just a specialized accumulator that always adds 1. Counting how many values are even is the same skeleton as summing them; only the update statement changes from sum += val to count++.
Array and ArrayList traversals (Unit 4)
Standard data-collection algorithms like finding the sum, average, max, or count of matching elements are accumulators applied across a collection. The traversal loop visits each element, and the accumulator remembers what it has seen. This combo is the backbone of the Array/ArrayList FRQ.
No released FRQ uses the phrase "accumulator variable" verbatim, but the pattern itself is everywhere. Multiple-choice questions test it through code tracing. You'll see a loop with a variable like sum, count, or total and be asked for its final value, which means tracking the accumulator iteration by iteration (LO 2.8.A asks you to determine the result of iterative processes). FRQs test it through writing. Methods that return a sum, an average, a count of elements meeting a condition, or a built-up String all require you to declare the accumulator with the right starting value, update it correctly inside the loop, and return it after the loop ends. The two most common point-losers are initializing inside the loop (which resets the accumulator every iteration) and returning inside the loop (which exits after one pass).
Both are variables tied to a loop, but they do opposite jobs. The loop control variable (the i in for (int i = 0; i < n; i++)) lives in the for loop header and determines how many times the loop runs (EK 2.8.A.2). The accumulator lives in the loop body and stores the running answer, like sum += arr[i]. In a typical loop, i is the control variable and sum is the accumulator. You can read i to compute the accumulator's update, but you shouldn't be storing your final answer in i.
An accumulator variable is initialized before a loop and updated inside the loop body so it builds a running result like a sum, count, or product.
Initialize sums and counts to 0, products to 1, and a max or min to the first element of the data, because a wrong starting value gives a wrong final answer.
The accumulator is different from the loop control variable in the for loop header; the control variable drives the iteration while the accumulator stores the result (EK 2.8.A.2).
Declaring the accumulator inside the loop body resets it every iteration and is one of the most common FRQ mistakes.
Return or use the accumulator after the loop finishes, not inside it, or you'll exit after a single iteration.
The same pattern powers later algorithms like summing an array, averaging an ArrayList, and building Strings, so mastering it in Unit 2 pays off across the course.
It's a variable initialized before a loop (like int sum = 0;) and updated inside the loop body each iteration (like sum += value;) to build a running total, count, or other combined result. It's part of Topic 2.8 (For Loops) in Unit 2.
No. The loop control variable (like i in a for loop header) controls how many times the loop runs, while the accumulator sits in the loop body and stores the result you're building. A loop summing an array uses both at once: i to walk the array and sum to hold the total.
A counter is a specific kind of accumulator that always increases by 1, like count++ whenever a condition is true. A general accumulator can add varying amounts, multiply, or even concatenate Strings.
No. Use 0 for sums and counts, but 1 for products (0 would zero out every multiplication) and the first data value when finding a max or min. Picking the wrong starting value is a classic way to lose FRQ points.
You probably declared it inside the loop body, so it gets re-created and re-initialized every iteration. Move the declaration and initialization above the loop so the value carries over between iterations.
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
Review units, study guides, and course resources.
Check this vocabulary in multiple-choice context.
Apply key concepts in written AP responses.
Estimate the exam score you are working toward.
Review the highest-yield facts before practice.
Put the full course together before test day.