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.

1.6.A1.6.A.11.6.A.2

Reviewed against the CED on

Know this already? Jump to the checkpoint.

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:

2

Lock 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

  1. The 5 compound assignment operators are +=, -=, *=, /= and %=.
  2. x += 3 does the same as x = x + 3: the operation, then the assignment.
  3. The whole right side is evaluated before the operation: x *= 2 + 3 is x = x * (2 + 3).
  4. x++ adds 1 and x-- subtracts 1, and the new value is stored in x.
  5. /= 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.

  1. 1 What is printed? 1.6.A.1, 1.6.A.2
    int n = 10;
    n += 4;
    n /= 3;
    n *= 2;
    n %= 5;
    n++;
    System.out.println(n);
    Show answer

    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.

  2. 2 Consider the following code segment. What is printed? 1.6.A.1
    int k = 5;
    k *= 3 - 1;
    System.out.println(k);
    Show answer

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

  3. 3 Consider the following code segment. What is printed? 1.6.A.1
    double total = 9.0;
    total /= 2;
    total += 1;
    System.out.println(total);
    Show answer

    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.

  4. 4 Consider the following code segment. What is printed? 1.6.A.2, 1.6.A.1
    int lives = 3;
    lives--;
    lives += 4;
    lives++;
    lives %= 4;
    System.out.println(lives);
    Show answer

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

3PrevailLock it in for exam day

By the numbers

Name each list from memory, then reveal it to check. Each list shows how many items to recall.

  • Compound assignment operators five operators CED 1.6.A.1
    1. +=
    2. -=
    3. *=
    4. /=
    5. %=
  • Increment and decrement operators (postfix form only) two operators CED 1.6.A.2
    1. x++add 1
    2. 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.

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure