Skip to main content

Unit 1 · Topic 1.6

1.6 Compound Assignment Operators

Compound assignment operators are shortcuts for updating a variable using its own current value. You'll use += and ++ constantly in loops for counters and running totals, so you need to trace them quickly and correctly.

Key terms

  • compound assignment operator
  • +=
  • increment ++
  • decrement --

The five compound assignment operators

Each compound operator takes the variable's current value, does one arithmetic operation with the value on the right, and stores the result back in the same variable.

ShortcutMeans the same asIf x starts at 10, x becomes
x += 3;x = x + 3;13
x -= 3;x = x - 3;7
x *= 3;x = x * 3;30
x /= 3;x = x / 3;3
x %= 3;x = x % 3;1

Two details that cause mistakes

First, the whole right-hand side is worked out before the operation. x *= 2 + 3; means x = x * (2 + 3), not x * 2 + 3. If x is 4, the result is 20.

Second, the type rules from 1.3 still apply. If x is an int, x /= 2; does integer division: 5 becomes 2. If y is a double holding 5.0, y /= 2; gives 2.5.

+= also works on strings: msg += "!"; makes a new string with an exclamation mark on the end and stores it back in msg.

Increment and decrement

count++; adds 1 to count, and count--; subtracts 1. They're the shortest way to update a counter, and you'll see i++ in almost every for loop header.

int lives = 3; lives--; lives--; lives++; String msg = "Lives"; msg += ": " + lives; System.out.println(msg);

lives goes 3, 2, 1, then back up to 2, and the last two lines build the string Lives: 2, which is printed.

Counters and running totals

Compound operators are the heart of two patterns you'll write over and over once you reach loops in Unit 2.

  • A counter starts at 0 and goes up by 1 each time something happens: count++;. At the end it tells you how many times the event occurred.
  • A running total (an accumulator) starts at 0 and adds each new value: total += price;. At the end it holds the sum.
  • A running product starts at 1, not 0, and multiplies: product *= n;. Starting at 0 would make every product 0.
  • A string can be built piece by piece the same way, starting from the empty string "" and using +=.

Not on the exam

On the exam, ++ and -- only appear after the variable and only as a statement on their own (or as a loop's update). You won't see the prefix form ++x, or ++ buried inside a bigger expression like arr[x++]. Those forms behave differently, so avoid them in your own code too.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Tracing a chain of updates

    What does this code print?int n = 10; n += 4; n /= 3; n *= 2 + 1; n %= 5; n--; System.out.println(n);

    Show the solution
    1. Step 1: Start: n is 10.
    2. Step 2: n += 4; makes it 14.
    3. Step 3: n /= 3; is integer division: 14 / 3 is 4 (the remainder 2 is dropped). n is 4.
    4. Step 4: n *= 2 + 1; works out the right side first, 3, so n is 4 × 3 = 12.
    5. Step 5: n %= 5; gives the remainder of 12 ÷ 5, which is 2.
    6. Step 6: n--; subtracts 1, so n is 1.

    Answer: It prints 1.

  2. Example 2

    All five operators

    What is printed?int score = 7; score += 5; score -= 2; score *= 3; score /= 4; score %= 5; System.out.println(score);

    Show the solution
    1. Step 1: 7 + 5 = 12, then 12 − 2 = 10, then 10 × 3 = 30.
    2. Step 2: score /= 4; is 30 / 4 with two ints, which is 7, not 7.5.
    3. Step 3: score %= 5; is 7 % 5, which is 2.

    Answer: It prints 2.

Common mistakes

  • Reading x *= 2 + 3 as x * 2 + 3. The whole right side is computed first.
  • Forgetting integer division with /= on an int variable.
  • Writing x =+ 3; instead of x += 3;. The first one compiles but just stores +3 in x, which is a logic error.

On the exam

  • Short tracing questions often chain several compound operators. Update a single value line by line and watch for integer division and %.
  • In free-response answers, count++; and total += value; are the standard ways to update counters and totals inside a loop.

Connected topics

Videos

  • AP Computer Science A - Topic 1.6 - Compound Assignment Operators

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Compound Assignment Operators

    CodeHSWatch on YouTube (opens in a new tab)

  • Arithmetic and Unary Operators (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • Java Compound Operators - Combined Assignment Arithmetic Operator Examples - Java Tutorial

    AppficialWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 1.6 Compound Assignment Operators. Pick an answer to see if you got it, and why.

Question 1 of 4

Consider the following code segment.int x = 10; x += 4; x /= 3; x *= 2; x %= 5; System.out.println(x);What is printed as a result of executing the code segment?

Question 2 of 4

Consider the following code segment.int count = 5; count++; count--; count++; count += count; System.out.println(count);What is printed as a result of executing the code segment?

Question 3 of 4

Consider the following code segment.double d = 7; int k = 7; d /= 2; k /= 2; System.out.println(d + " " + k);What is printed as a result of executing the code segment?

Question 4 of 4

Assume score is an int variable that has been given a value. Which of the following statements has the same effect as score = score - 3;?

0 of 4 answered