01 / 21
+= -= *=

Compound Assignment
Operators

Master the shorthand operators that combine arithmetic and assignment into a single, elegant expression in Java.

AP Computer Science A

What Are Compound Assignment Operators?

A compound assignment operator combines an arithmetic operation with the assignment operator (=) into a single step. Instead of writing the variable name twice, you write it once.

// Without compound assignment score = score + 10; // With compound assignment score += 10; // Same result!

The Five Compound Operators

+=
Add and assign: \( x \mathrel{+}= n \)
-=
Subtract and assign: \( x \mathrel{-}= n \)
*=
Multiply and assign: \( x \mathrel{*}= n \)
/=
Divide and assign: \( x \mathrel{/}= n \)
%=
Modulo and assign: \( x \mathrel{\%}= n \)

Why Use Compound Operators?

1
Conciseness: Less code to write and read. You eliminate repeating the variable name.
2
Clarity of intent: count += 1 clearly says "increment count," while count = count + 1 forces you to parse both sides.
3
Fewer errors: With long variable names, writing the name only once removes the risk of a typo on one side.
4
AP Exam standard: The AP CSA exam expects you to read and write compound assignment operators fluently.

The General Rule

COMPOUND FORM
\( \text{variable} \ \textcolor{#fbbf24}{\texttt{op}}\texttt{=}\ \text{expression}; \)
equivalent to EXPANDED FORM
\( \text{variable} = \text{variable} \ \textcolor{#fbbf24}{\texttt{op}}\ \text{expression}; \)

Hidden Feature: Implicit Casting

Compound assignment operators include an implicit type cast to the variable's type. This is a subtle but important difference from the expanded form.

int x = 5; x += 2.7; // x becomes 7 (truncates to int) // The expanded equivalent would NOT compile: // x = x + 2.7; ← ERROR: lossy conversion double→int // Compound version is equivalent to: x = (int)(x + 2.7); // This compiles

+= vs = ... +: Key Differences

\( \textcolor{#818cf8}{x \mathrel{+}= \text{expr}} \) \( \textcolor{#f87171}{x = x + \text{expr}} \)
Characters typed Fewer (variable name once) More (variable name twice)
Implicit cast? Yes: casts result to type of x No: may cause compile error
Variable evaluated Once Twice
Behavior Identical result when types match Identical result when types match
AP Exam Expected Also valid

How to Apply Compound Operators

1
Identify the pattern: Look for \( \text{variable} = \text{variable} \ \texttt{op}\ \text{expression} \). Both occurrences of the variable must be the same.
2
Extract the operator: What arithmetic operation is being performed? (+, -, *, /, %)
3
Combine: Write \( \text{variable} \ \texttt{op=}\ \text{expression} \). Drop the second occurrence of the variable.
4
Verify types: If the variable is int and the expression is double, the compound form will truncate. Make sure that's intended.

Example 1: Basic Conversion

Convert each expanded statement to its compound form.

// Expanded form → Compound form total = total + 25; → total += 25; health = health - 10; → health -= 10; price = price * 1.08; → price *= 1.08; pizza = pizza / 8; → pizza /= 8; leftover = leftover % 3; → leftover %= 3;

Example 2: Tracing Values

Trace the value of x after each line executes:

int x = 10; // x = 10 x += 5; // x = 10 + 5 = 15 x *= 2; // x = 15 * 2 = 30 x -= 7; // x = 30 - 7 = 23 x /= 4; // x = 23 / 4 = 5 (integer division!) x %= 3; // x = 5 % 3 = 2

Example 3: Loop Accumulation

Compound operators shine in loops. Here we accumulate a running total:

int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; // adds i to sum each iteration } // Iteration trace: // i=1: sum = 0+1 = 1 // i=2: sum = 1+2 = 3 // i=3: sum = 3+3 = 6 // i=4: sum = 6+4 = 10 // i=5: sum = 10+5 = 15

Example 4: String Concatenation with +=

The += operator also works with Strings. It appends text:

String message = "Hello"; message += " "; // "Hello " message += "World"; // "Hello World" message += "!"; // "Hello World!" // You can also append numbers (auto-converts to String): String result = "Score: "; result += 95; // "Score: 95"

Watch the Order of Operations

The entire right-hand expression is evaluated first, then the operation is applied. This matters with complex expressions:

int x = 10; x *= 2 + 3; // x *= (2 + 3) → x = 10 * 5 = 50 // NOT: x = (10 * 2) + 3 = 23 int y = 20; y /= 4 - 2; // y /= (4 - 2) → y = 20 / 2 = 10 // NOT: y = (20 / 4) - 2 = 3

How x += 5 Executes

STEP 1: Read x → 10 STEP 2: Compute
\(10 + 5 = 15\)
STEP 3: Store x ← 15 MEMORY BEFORE x 10 AFTER x 15 The old value (10) is overwritten with the new value (15)

Common Mistakes

✕ Space in Operator
Writing + = (with a space) instead of +=. The operator must be a single token: no space between + and =.
✕ Wrong Order
Writing =+ instead of +=. In Java, x =+ 5 assigns positive 5 to x (unary +), not x + 5.
✕ Forgetting Precedence
Assuming x *= 2 + 3 means (x*2) + 3. It actually means x * (2+3). The full right side is evaluated first.
✕ Using on Undeclared Variables
Writing y += 10; before declaring y. The variable must exist and be initialized before a compound assignment.

Summary Reference Table

Operator Example Equivalent Result (x=20)
+= \( x \mathrel{+}= 5 \) \( x = x + 5 \) 25
-= \( x \mathrel{-}= 8 \) \( x = x - 8 \) 12
*= \( x \mathrel{*}= 3 \) \( x = x * 3 \) 60
/= \( x \mathrel{/}= 6 \) \( x = x / 6 \) 3 (int division)
%= \( x \mathrel{\%}= 7 \) \( x = x \% 7 \) 6

Question 1 of 5

What is the value of x after the following code executes?

int x = 12; x += 8;

Question 2 of 5

What is the value of y after this code runs?

int y = 25; y %= 7;

Question 3 of 5

What does \( x \mathrel{*}= 3 + 2 \) evaluate to when \(x\) is currently 4?

Question 4 of 5

Which of the following is NOT equivalent to \( \text{count} = \text{count} - 1; \)?

Question 5 of 5

After the following code, what is the value of n?

int n = 100; n /= 3; n *= 3;