AP Computer Science A · Unit 1 lecture · Part 5 of 11 · 5 min

1.6 Compound assignment operatorsShortcuts that update a variable

Mr. Thompkins
Short one. Programmers update variables so often that Java gives you shortcuts. They save typing, and they're easy to trace once you translate each one back into its long form.

A compound assignment operator does an operation and an assignment in one step. x += 5; means x = x + 5;. The same pattern works for every arithmetic operator: -=, *=, /= and %=. The usual type rules still apply, so if x is an int, x /= 2; is integer division.

Adding or subtracting exactly 1 is so common it has its own operators: x++; adds 1 to x, and x--; subtracts 1. On the AP exam these are used only as stand-alone statements, never buried inside a bigger expression. (Java also has ++x, but it's outside the AP subset.)

?Wait, why does x = x + 5 make sense?
Because = means "evaluate the right, then store on the left." Java reads the current value of x, adds 5, and stores the sum back in x. If x was 10, it's now 15. The old 10 is replaced.
StatementSame aspts after
int pts = 10;10
pts += 5;pts = pts + 5;15
pts *= 3;pts = pts * 3;45
pts -= 4;pts = pts - 4;41
pts /= 2;pts = pts / 2;20
pts %= 7;pts = pts % 7;6
pts++;pts = pts + 1;7
Each shortcut translated into its long form, traced one line at a time. Notice the /= line: 41 / 2 is 20 with ints.
?Wait, what does % do again?
a % b is the remainder when a is divided by b. 20 % 7 is 6, because 7 goes into 20 twice (14) with 6 left over. If a is smaller than b, the answer is just a: 3 % 7 is 3.
Pause and think
int n = 7; n /= 2; n *= 2; What is n now, and why isn't it 7?
I thought about it. Show me
n is 6. n /= 2 is integer division, so n becomes 3, and the .5 is lost for good. Then n *= 2 makes it 6. Dividing and multiplying by the same number only undoes itself with doubles.
Trace the shortcuts

What is printed?

int coins = 23;
coins %= 10;
coins *= 4;
coins++;
coins -= 20;
coins /= 2;
System.out.println(coins);
  1. coins %= 10 means coins = 23 % 10, which is 3.
  2. coins *= 4 makes it 12. coins++ makes it 13.
  3. coins -= 20 makes it 13 - 20, which is -7.
  4. coins /= 2 is -7 / 2 with ints. The exact answer is -3.5, and truncating toward zero gives -3.
    Why?
    Truncation moves toward zero, so negative results round up toward zero, not down. -7 / 2 is -3, not -4.

Answer: -3

Stuck? Get a hint
  1. Rewrite each line in its long form: coins = coins % 10; and so on.
  2. Keep a column with the value of coins after each line.
  3. The last step is -7 / 2 with two ints. Which way does truncation go?
Check yourself
What is printed?
double d = 5;
int k = 5;
d /= 2;
k /= 2;
System.out.println(d + " " + k);
  1. A2.5 2
  2. B2.5 2.5
  3. C2 2
  4. D2.0 2
Show the answer

A. d is a double, so d /= 2 is 5.0 / 2, which is 2.5. k is an int, so k /= 2 is 5 / 2, which is 2. Choice B is the trap: the shortcut doesn't change the rules, so int division still truncates.

Exam tip from Mr. T

Long chains of updates are trace-table questions. Make one column per variable and one row per line, and write the value after every line. Students who do this in their heads are the ones who pick the trap answer.