A compound assignment operator updates a variable using its own current value: x += 5; does the same as x = x + 5;, and -=, *=, /= and %= work the same way with their own operators. The statements x++; and x--; add 1 to x and subtract 1 from it. To find a variable's final value, trace the statements in order with a table, because each one starts from the value the last one left.
Quick check
Both operands are int. What is the value of 17 / 5 in Java?Show answerHide answer
intdivision drops the fractional part of 3.4.
x holds 4 and the statement x = x + 3; runs. What does x hold now?Show answerHide answer
- The right side uses the old value, 4, and the result replaces it.
Know these already? Jump to the practice questions
What you'll be able to do
- State the plain assignment that each of
+=,-=,*=,/=,%=,++and--stands for. - Determine a variable's value after a sequence of compound assignments, using a trace table.
- Explain why
x *= a + bmultipliesxby the whole ofa + b. - Identify a statement that uses the wrong operator for the update a task needs.
Key terms
Compound assignment operator
An operator such as += that applies an arithmetic operator to a variable and the value on its right, then stores the result back in the variable.
- In plain English
- A shortcut for "update this variable using its current value".
- Don't write
- "
x += 2adds 2 toxwithout changingx."
Increment and decrement operators
++ adds 1 to a variable and -- subtracts 1; the statement n++; does the same as n += 1;.
- In plain English
- Count up by one, or count down by one.
- Don't write
- "
n++adds 2 ton, one for each plus sign."
Trace table
A table with one column per variable and one row per statement, showing each value after that statement runs.
- In plain English
- A record of every change, line by line.
- Don't write
- "Only the final value needs tracing."
What does a compound assignment operator do?
Each operator stands for a plain assignment. Starting each time from x = 17:
| Statement | Same as | x afterwards |
|---|---|---|
x += 3; | x = x + 3; | 20 |
x -= 3; | x = x - 3; | 14 |
x *= 3; | x = x * 3; | 51 |
x /= 3; | x = x / 3; | 5 |
x %= 3; | x = x % 3; | 2 |
If x is an int, /= is int division, so 17 / 3 truncates to 5, and %= keeps the remainder, 2. If x is a double, /= keeps the fraction.
The whole right side is worked out first. x *= 2 + 3; means x = x * (2 + 3);, so with x at 4 the result is 20, not 4 * 2 + 3 = 11.
Learn this: these operators are Java syntax, not on the Java Quick Reference.
Key idea
x op= expression means x = x op (expression), with the result converted back to the type of x: the right side is evaluated in full before it meets x.
Check yourself
Show answerHide answer
n = n / 2isintdivision, and 4.5 truncates to 4.
What do ++ and -- do?
count++; adds 1 to count, and count--; subtracts 1. They are the shortest way to write count += 1; and count -= 1;, and the loops of Unit 2 use them to count.
int lives = 3;
lives--;
lives--;
lives++;
lives goes 3, 2, 1, 2.
Here ++ and -- are always used as statements on their own, such as lives--;. Each one changes the variable by exactly 1, whatever its type: a double holding 2.5 becomes 3.5 after ++.
Check yourself
Show answerHide answer
- The value goes 0, 1, 2, 1.
How do you trace a sequence of updates?
Make one column per variable and one row per statement. For each row, copy the values from the row above and change only the variable on the left of that statement.
int a = 6;
int b = 4;
a += b;
b *= 3;
a -= b;
b %= 5;
a++;
| Statement | a | b |
|---|---|---|
| start | 6 | 4 |
a += b; | 10 | 4 |
b *= 3; | 10 | 12 |
a -= b; | -2 | 12 |
b %= 5; | -2 | 2 |
a++; | -1 | 2 |
Row 4 uses the new b, 12, not the starting 4. Reading the row above every time is what stops you using a stale value.
Check yourself
Show answerHide answer
6, because 10 − 4 = 6. The correct value is −2, using the updated b of 12.
Worked example 1: Trace one variable through every operator
Question: What is printed when this code segment runs?
int score = 10;
score += 7;
score *= 2;
score /= 4;
score %= 3;
score--;
System.out.println(score);
What tells you the method
A chain of updates to one variable: trace it with one row per statement.
Step 1: Trace each statement.
Statement scorestart 10 score += 7;17 score *= 2;34 score /= 4;8 score %= 3;2 score--;1 Whyscoreis anint, so 34 / 4 truncates 8.5 to 8, and 8 % 3 leaves 2.Step 2: Print the final value.
printlnprints 1.WhyOnly the value after the last update is printed.
Check it
Re-trace with plain assignments: 10 + 7 = 17, 17 × 2 = 34, 34 / 4 = 8, 8 % 3 = 2 and 2 − 1 = 1.
Answer
1
Worked example 2: Trace two variables of different types
Question: What is printed when this code segment runs?
double total = 20.0;
int items = 3;
total -= 5;
items++;
total /= items;
items *= items;
System.out.println(total);
System.out.println(items);
What tells you the method
Two variables of different types: trace both columns, and note which divisions are double.
Step 1: Trace the statements.
Statement totalitemsstart 20.0 3 total -= 5;15.0 3 items++;15.0 4 total /= items;3.75 4 items *= items;3.75 16 Whytotalis adouble, so 15.0 / 4 keeps the fraction;items *= itemssquares 4.Step 2: Print both.
3.75, then 16.
WhyThe division used the updated
items, 4, from the row above.
Check it
3.75 × 4 = 15.0, which matches total before the division.
Answer
3.75 then 16, one per line.
Worked example 3: Evaluate the right side first
Question: int x = 5; and int y = 2; are declared. What value does x hold after x *= y + 1; runs?
- A) 8
- B) 10
- C) 11
- D) 15
What tells you the method
A compound operator with an expression on the right: rewrite it as a plain assignment with parentheses.
Step 1: Rewrite the statement.
x *= y + 1;meansx = x * (y + 1);.WhyA compound assignment evaluates the whole right side before applying its operator.
Step 2: Evaluate.
y + 1is 3, and 5 × 3 = 15.WhyThe parentheses come from the operator's meaning, not from precedence in the original line.
Check it
Each distractor comes from one slip: 8 treats *= as +=, 10 drops the + 1, and 11 computes x * y + 1.
Answer
D
Your turn
Question: What is printed when this code segment runs?
int n = 50;
n -= 8;
n /= 5;
n %= 3;
n++;
System.out.println(n);
Step 1: First two updates.
50 − 8 = 42, then 42 / 5 is 8 in
intdivision.Step 2: Your go: finish the trace and give the printed value.
Show the full solutionHide the solution
Step 2. 8 % 3 is 2, because 8 = 2 × 3 + 2. Then n++ makes it 3.
Answer: 3
Traps
Trap
x *= a + b; multiplies x by a and then adds b.
It seems right because * normally comes before +. But a compound assignment evaluates its whole right side first, so the statement means x = x * (a + b);.
m holds 3. What does m hold after m *= 4 + 1;?
15, because 3 × (4 + 1) = 15.
Trap
t /= 2; on an int keeps the half.
It seems right because "divide by 2" sounds exact. But for an int variable it is int division, so 7 becomes 3.
An int t holds 7. What does it hold after t /= 2;?
- 7 / 2 truncates 3.5 to 3.
Trap
x =+ 3; adds 3 to x.
It seems right because it uses the same two symbols as +=. But the order matters: =+ 3 is a plain assignment of the value +3, so x becomes 3 whatever it held before.
x holds 10. What does it hold after x =+ 3;?
- The statement assigns +3; it does not add.
What's examined
On the AP® Computer Science A exam, Unit 1 compound assignment appears in multiple-choice questions that ask for a variable's value or the printed output after a sequence of statements, and += and ++ reappear in the loops of Unit 2. A trace table is the reliable method.
Practice questions
- Recall Which statement has the same effect as
total = total * 4;?
- A)
total *= 4; - B)
total =* 4; - C)
total += 4; - D)
total * 4;
Show answer
Answer: A. *= multiplies total by 4 and stores the result back.
Why the others are wrong:
- B has the symbols in the wrong order and does not compile.
- C uses the wrong operator, so it adds 4.
- D computes a value but stores it nowhere, so it does not compile as a statement.
- Apply What is printed when this code segment runs?
int k = 9;
k += 6;
k %= 4;
k *= 10;
k--;
System.out.println(k);
Show answer
Answer: 29
Marking points:
kgoes 9, 15, 3, 30, 29.- 15 % 4 is 3, because 15 = 3 × 4 + 3.
- Apply What value does
phold after this code segment runs?
int p = 3;
p *= p;
p += p;
Show answer
Answer: 18
Marking points:
p *= pgives 3 × 3 = 9.p += pgives 9 + 9 = 18.
- Exam-style What value does
xhold after this code segment runs?
int x = 20;
x -= 2 * 3;
x /= 4;
x++;
- A) 3
- B) 4
- C) 4.5
- D) 14
Show answer
Answer: B. 20 − 6 = 14, then 14 / 4 = 3 in int division, then 3 + 1 = 4.
Why the others are wrong:
- A forgets the final
x++. - C uses true division, 3.5, which an
intvariable cannot hold. - D computes
(x - 2) * 3first, giving 54, then 13, then 14.
- Exam-style This code segment is meant to split 200 seconds into whole minutes and leftover seconds, printing 3 and then 20. Give the number of the line that stops it working.
1: int seconds = 200;
2: int minutes = seconds;
3: minutes /= 60;
4: seconds /= 60;
5: System.out.println(minutes);
6: System.out.println(seconds);
Show answer
Answer: Line 4.
Marking points:
- Line 3 is correct: 200 / 60 is 3.
- Line 4 divides again, so
secondsbecomes 3 and the code prints 3 twice. - Changing line 4 to
seconds %= 60;leaves the remainder, 20.
- Exam-style What values do
aandbhold after this code segment runs?
int a = 4;
int b = 7;
a += b;
b -= a;
a *= b;
- A)
ais 44 andbis 4 - B)
ais 11 andbis -4 - C)
ais -44 andbis -4 - D)
ais 28 andbis 7
Show answer
Answer: C. a becomes 11, then b becomes 7 − 11 = −4, then a becomes 11 × −4 = −44.
Why the others are wrong:
- A subtracts the wrong way round, 11 − 7, in the second update.
- B stops before the last statement.
- D uses the starting values in every statement.
- Recall What is the name of the operator
--in the statementcount--;?
Show answer
Answer: The decrement operator.
Marking points:
- It subtracts 1 from
count, the same ascount -= 1;.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
x op= emeansx = x op (e), converted back to the type ofx, with the whole ofeworked out first./=and%=on anintvariable useintdivision and the remainder.x++;andx--;changexby exactly 1.- Trace with one row per statement, always reading the values from the row above.
=+is not+=: it assigns a positive value instead of adding.