Topic 1.6 Notes – Compound Assignment Operators
What Compound Assignment Operators Do
You already know a basic assignment:
x = x + 4;
A compound assignment operator compresses that idea:
x += 4;
General pattern:
variable op= expression;
means the same as:
variable = variable op expression;
Here’s the flow when you run something like x += 4;:

Execution flow of a compound assignment statement
Important details:
- The right side is evaluated first.
- The operation uses the variable’s current value.
- The result replaces the old value in that variable.
- The variable must already be declared and initialized.
If you try this:
int total;
total += 5; // error
Java complains because total has no starting value to use in the calculation.
The Five Compound Assignment Operators
All five follow the same structure. What changes is the operation.
+= Add and assign
sum += 7;
- Adds 7 to the current value of
sum. - Common for totals, counters, accumulating values in loops.
- Also works with Strings:
String name = "AP";
name += " CSA"; // "AP CSA"
With strings, it concatenates.
-= Subtract and assign
balance -= 20;
- Decreases a value.
- Used for score deductions, removing quantities, etc.
*= Multiply and assign
price *= 2;
- Scales a number.
- Often seen in growth or repeated multiplication.
/= Divide and assign
count /= 3;
- Divides the current value.
- If
countis anint, integer division rules apply.
Example:
int x = 7;
x /= 2; // x becomes 3
Decimals are truncated.
%= Modulus and assign
num %= 4;
- Stores the remainder.
- Helpful for wrapping values or checking cycles.
Type Rules You Need to Know
- Work with
intanddouble. +=works with String.- Automatic casting can happen:
int x = 5;
x += 2.9; // x becomes 7
The decimal is truncated when stored back into an int.
That truncation is a common multiple-choice trap.
Increment and Decrement Operators
These are specialized compound assignments.
Post-increment x++
Equivalent to:
x += 1;
Adds 1 to the variable and stores it back.
Post-decrement x--
Equivalent to:
x -= 1;
Subtracts 1 and stores it back.
On the AP exam:
- You will only see post form (
x++,x--). - You will not see
++x. - You will not see it embedded in expressions like
arr[i++].
They are usually standalone statements, especially in loops.
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Each loop iteration increases i by 1.
Tracing Compound Assignments Correctly
When you see code like this on a quiz:
int a = 4;
int b = 3;
a *= b + 2;
b -= 1;
a += b;
Trace it line by line:
Start: a = 4, b = 3
a *= b + 2
b + 2 = 5
a = 4 * 5 = 20
b -= 1
b = 2
a += b
a = 20 + 2 = 22
Final values: a = 22, b = 2.
Students often mess up by forgetting that a was already changed before the last line.
Where This Shows Up
You’ll see compound assignment constantly in:
- Running totals →
sum += arr[i]; - Counting matches →
count++; - Updating instance variables inside methods
- Loop control variables →
i++
If you’re writing a loop on an FRQ, you’re almost guaranteed to use ++ or +=.