# AP® Computer Science A 1.6 Compound Assignment Operators

Source: https://lockincs.com/ap-csa/unit-1/1-6-compound-assignment-operators/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.6.A, 1.6.A.1, 1.6.A.2

A compound assignment operator (+=, -=, *=, /= or %=) does the arithmetic between the variable on the left and the value on the right, then stores the result in the variable. The post-increment operator ++ adds 1 to a numeric variable and the post-decrement operator -- subtracts 1.

## What you will learn

- 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.

## 1\. The five compound assignment operators

The compound assignment operators `+=`, `-=`, `*=`, `/=` and `%=` can be used in place of the assignment operator in numeric expressions. A compound assignment operator takes the current value of the variable on the left, does the arithmetic with the value on the right, and stores the result back in that variable 1.6.A.1.

| Compound form | Same as |
| --- | --- |
| `x += 3;` | `x = x + 3;` |
| `x -= 3;` | `x = x - 3;` |
| `x *= 3;` | `x = x * 3;` |
| `x /= 3;` | `x = x / 3;` |
| `x %= 3;` | `x = x % 3;` |

Each line uses the value left by the line before:

```
int k = 7;
k += 3;    // 10
k *= 2;    // 20
k -= 5;    // 15
k /= 4;    // 3 (int division)
k %= 3;    // 0
System.out.println(k);
```

Output:

```
0
```

`k /= 4` follows the same int division rule as `/`: 15 / 4 is 3.

## 2\. The right side comes first

The whole right side is worked out first. If `x` is 4, `x *= 2 + 3` means `x = x * (2 + 3)`, so `x` becomes 20, not 11 1.6.A.1.

```
int x = 4;
x *= 2 + 3;
System.out.println(x);
double price = 10.0;
price -= 2.5 * 2;
System.out.println(price);
```

Output:

```
20
5.0
```

**Watch out:** `/=` and `%=` on an `int` variable keep int arithmetic, so the decimal part is lost. On a `double` variable, `/=` keeps the decimals.

## 3\. Increment and decrement

The post-increment operator `++` adds 1 to the stored value of a numeric variable, and the post-decrement operator `--` subtracts 1. The new value is assigned to the variable 1.6.A.2.

```
int lives = 3;
lives++;   // 4
lives++;   // 5
lives--;   // 4
System.out.println("Lives: " + lives);
```

Output:

```
Lives: 4
```

Outside the course: the prefix form `++x`, and using `++` or `--` inside another expression, such as `arr[x++]`. On the exam, `x++` and `x--` appear as statements on their own.

## 4\. Tracing a full sequence

Trace one line at a time and write the new value after each line.

```
int score = 12;
score += 8;
score /= 3;
score *= 4;
score %= 7;
score--;
System.out.println(score);
```

| Line | Code | score |
| --- | --- | --- |
| 1 | `int score = 12;` | 12 |
| 2 | `score += 8;` | 20 |
| 3 | `score /= 3;` | 6 (int division: 20 / 3) |
| 4 | `score *= 4;` | 24 |
| 5 | `score %= 7;` | 3 |
| 6 | `score--;` | 2 |

Output:

```
2
```

## Common mix-ups

- Not true: x *= 2 + 3 means x = x * 2 + 3. The whole right side is worked out first. x *= 2 + 3 means x = x * (2 + 3).
- Not true: k /= 4 keeps the decimal part. If k is an int, /= uses int division, the same as /. If k is 15, k /= 4 stores 3.
- Not true: x++ and x + 1 do the same thing. x + 1 is only an expression; it does not change x. x++ stores the new value in x.

## In 30 seconds

- The 5 compound assignment operators are +=, -=, *=, /= and %=.
- x += 3 does the same as x = x + 3: the operation, then the assignment.
- The whole right side is evaluated before the operation: x *= 2 + 3 is x = x * (2 + 3).
- x++ adds 1 and x-- subtracts 1, and the new value is stored in x.
- /= and %= on an int variable keep int arithmetic.

## Checkpoint: 4 practice questions

### Question 1

What is printed?

```java
int n = 10;
n += 4;
n /= 3;
n *= 2;
n %= 5;
n++;
System.out.println(n);
```

- A. 5
- B. 3
- C. 4
- D. 10

**Answer: C.** 14, then 14 / 3 = 4, then 8, then 8 % 5 = 3, then 4. B forgets `n++`. A keeps the decimals of 14 / 3 (4.67, 9.33, 4.33, 5.33) and rounds the final value. D is the starting value, as if the compound operators did not store their results.

### Question 2

Consider the following code segment. What is printed?

```java
int k = 5;
k *= 3 - 1;
System.out.println(k);
```

- A. 10
- B. 14
- C. 15
- D. 3

**Answer: A.** The whole right side is evaluated first: 3 - 1 is 2, and 5 * 2 is 10. B computes 5 * 3 - 1 = 14, ignoring that the right side is grouped. C uses only the 3. D (3) subtracts instead of multiplying: 5 - (3 - 1).

### Question 3

Consider the following code segment. What is printed?

```java
double total = 9.0;
total /= 2;
total += 1;
System.out.println(total);
```

- A. 5
- B. 5.5
- C. 4.5
- D. 5.0

**Answer: B.** `total` is a double, so 9.0 / 2 is 4.5, and 4.5 + 1 is 5.5. A and D use int division (9 / 2 = 4), which only happens when both values are int; A also prints a double without its decimal point. C forgets the `+= 1`.

### Question 4

Consider the following code segment. What is printed?

```java
int lives = 3;
lives--;
lives += 4;
lives++;
lives %= 4;
System.out.println(lives);
```

- A. 7
- B. 2
- C. 0
- D. 3

**Answer: D.** 3, then 2, then 6, then 7, then 7 % 4 = 3. A forgets the `%= 4`. B forgets `lives++` (6 % 4 is 2). C skips `lives--` (3 + 4 + 1 is 8, and 8 % 4 is 0).

## Questions students ask

**What does += mean in Java?**

x += 3 adds 3 to the current value of x and stores the result back in x. It does the same as x = x + 3.

**What is the difference between x++ and ++x?**

Both add 1 to x. The AP® course only uses the postfix form x++ as a statement on its own. The prefix form ++x, and using ++ or -- inside another expression, are outside the course.

**Does /= do integer division?**

Yes, when the variable is an int. k /= 4 means k = k / 4, so with k equal to 15 the result is 3.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
