Unit 1 · 10 min
1.6 Compound Assignment Operators
Shortcuts that do the arithmetic and the assignment in one step.
Summary, objectives and course and exam description (CED) codes
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.
By the end of this session you can
- 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.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
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:
2Lock it in: memory hooks
- Compound assignmentx op= y is short for x = x op (y): the right side is wrapped in parentheses.
- ++ and --Plus plus, one more; minus minus, one less.
Common misconceptions
"x *= 2 + 3 means x = x * 2 + 3."
Not quite. The whole right side is worked out first. x *= 2 + 3 means x = x * (2 + 3).
"k /= 4 keeps the decimal part."
Not quite. If k is an int, /= uses int division, the same as /. If k is 15, k /= 4 stores 3.
"x++ and x + 1 do the same thing."
Not quite. x + 1 is only an expression; it does not change x. x++ stores the new value in x.
Exam traps
- /= and %= on an int variable keep int arithmetic, so the decimal part is lost.
- The whole right side is worked out first: if x is 4, x *= 2 + 3 makes x 20, not 11.
- Each line uses the value left by the line before it. Trace one line at a time.
2PracticeRecall it, then check yourself
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.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 4 questions
Checkpoint: Compound Assignment Operators
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
3PrevailLock it in for exam day
By the numbers
- Compound assignment operators five operators CED 1.6.A.1
- +=
- -=
- *=
- /=
- %=
- Increment and decrement operators (postfix form only) two operators CED 1.6.A.2
- x++add 1
- x--subtract 1
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Compound assignment operatortap to flip
One of +=, -=, *=, /= and %=: it does the arithmetic with the variable and the right side, then stores the result in the variable.
Example and CED codes -
Increment and decrement operatorstap to flip
The post-increment operator ++ adds 1 to a numeric variable and the post-decrement operator -- subtracts 1.
Example and CED codes -
Assignment statementtap to flip
A statement that uses = to store the value of the expression on the right in the variable on the left.
Example and CED codes -
Integer divisiontap to flip
Dividing two int values: the result is only the integer part of the quotient, so 7 / 2 is 3.
Example and CED codes
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.