From the free course AP Computer Science A on Lykke
1.6 Compound Assignment Operators
Key concepts: Compound assignment operators · Assignment statements · Determining the value stored in a variable · Step-by-step changes to variable values · Operator precedence · Multiplication, division, and remainder precedence over addition and subtraction
A statement such as score += 10; does two jobs at once: it calculates a new value from the current score, then stores that result back in score.
1.6 Compound Assignment Operators
A statement such as score += 10; does two jobs at once: it calculates a new value from the current score, then stores that result back in score. Compound assignment operators make repeated updates compact without hiding the underlying arithmetic.
Learning Objective 1.6.A: Develop code for assignment statements with compound assignment operators and determine the value that is stored in the variable as a result.
An assignment statement evaluates the expression on its right-hand side first, then stores the resulting value in the variable on its left-hand side. The left side identifies where the value goes; the right side determines what value is stored.
int points = 24;
points += 6;
The second statement uses +=. Java reads the current value of points, adds 6, and assigns the result back to points:
$$ \texttt{points += 6} \quad\text{means conceptually}\quad \texttt{points = points + 6} $$
After the statement, points stores $30$.
The five compound assignment operators
Essential Knowledge 1.6.A.1 identifies five compound assignment operators. Each performs an arithmetic operation between the value currently stored on the left and the value on the right, then assigns the result to the variable on the left.
| Compound form | Conceptual operation | Meaning |
|---|---|---|
x += y |
x = x + y |
increase x by y |
x -= y |
x = x - y |
decrease x by y |
x *= y |
x = x * y |
multiply x by y |
x /= y |
x = x / y |
divide x by y |
x %= y |
x = x % y |
store the remainder after division |
These are conceptual equivalents for understanding the operation, especially in ordinary int examples. Java compound assignment also applies an implicit conversion to the type of the variable on the left. Therefore, x += y is not always exactly interchangeable with writing x = x + y directly when different numeric types are involved.
Worked example: updating a travel budget
Suppose a travel program begins with a budget of $120. It pays a $35 fee, doubles the remaining amount through a matching grant, and then divides the result among four travelers.
int budget = 120;
budget -= 35;
budget *= 2;
budget /= 4;
Trace the variable after each statement:
budget = 120budget -= 35gives $120 - 35 = 85$budget *= 2gives $85 \times 2 = 170$budget /= 4gives $170 / 4 = 42$
The final stored value is 42. Because both operands in the division are integers, Java performs integer division: it discards the fractional part rather than storing $42.5$.
A shorter example makes the same rule visible:
int minutes = 17;
minutes /= 4;
After the compound assignment, minutes stores $4$, not $4.25$.
Remainder updates and changing values
The %= operator is useful when a program needs the remainder left after division. For example:
int items = 29;
items %= 6;
The expression items % 6 has value $5$, because $29$ divided by $6$ leaves remainder $5$. The assignment then replaces the old value, so items stores $5$.
Misconception check — “The operator changes only the right side.”
In count += 3, the 3 does not get changed. The variable count is updated. Compound assignment always stores the newly calculated result back into the variable on the left.
Precedence inside compound assignments
The right side of an assignment may be a larger expression. Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence are evaluated from left to right.
int value = 8;
value += 3 + 4 * 2;
First evaluate the right-hand expression. The multiplication occurs before the addition:
$$ 3 + 4 \times 2 = 3 + 8 = 11 $$
Then apply the compound assignment:
$$ 8 + 11 = 19 $$
Therefore, value stores $19$.
For operators of equal precedence, move from left to right:
int result = 20 / 5 * 2;
The division occurs first because it appears first:
$$ 20 / 5 \times 2 = 4 \times 2 = 8 $$
Parentheses can make a different grouping explicit, such as 20 / (5 * 2), which produces $2$.
Retrieval check
What value is stored in n after this code?
int n = 14;
n += 3 * 2;
n %= 5;
First, $3 \times 2 = 6$, so n becomes $20$. Then $20 \mathbin{%} 5 = 0$. The final value is $0$.
Skill 3.A — Determine the result or output based on statement execution order in an algorithm. Compound assignment questions assess this skill directly: evaluate the right-hand expression using precedence rules, perform the indicated operation, store the new value, and use that updated value in the next statement. An attempt to divide an integer by the integer zero results in an ArithmeticException, so a trace involving /= must also check that its divisor is not zero.

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A