5m left·0%
Reading Time: 5 min
Last Updated: June 19, 2026
Main Ideas: 5
Reading Time: 5 min
Last Updated: June 19, 2026
Main Ideas: 5

Topic 1.6 Notes – Compound Assignment Operators

Verified for 2027 AP® Computer Science A Exam
Read aloud
Compound assignment operators combine arithmetic and assignment into one step. Instead of rewriting a variable’s name on both sides of an equals sign, you update it in place. This topic also includes the increment (++) and decrement (--) operators, which are shortcuts for adding or subtracting 1.

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 count is an int, 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 int and double.
  • += 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 +=.

Key Takeaways

x op= y always means x = x op y, using the current value of x.
The right-hand expression is fully evaluated before the assignment happens.
Compound assignment permanently changes the variable’s value.
Integer division still truncates when using /=.
+= works with Strings for concatenation.
On the AP exam, only x++ and x-- in post form appear, and never inside complex expressions.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining