AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-6)
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.
| Shortcut | Means the same as | If 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.
- 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 solutionHide the solution
- Step 1: Start:
nis 10. - Step 2:
n += 4;makes it 14. - Step 3:
n /= 3;is integer division: 14 / 3 is 4 (the remainder 2 is dropped).nis 4. - Step 4:
n *= 2 + 1;works out the right side first, 3, sonis 4 × 3 = 12. - Step 5:
n %= 5;gives the remainder of 12 ÷ 5, which is 2. - Step 6:
n--;subtracts 1, sonis 1.
Answer: It prints
1. - Step 1: Start:
- 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 solutionHide the solution
- Step 1: 7 + 5 = 12, then 12 − 2 = 10, then 10 × 3 = 30.
- Step 2:
score /= 4;is 30 / 4 with twoints, which is 7, not 7.5. - Step 3:
score %= 5;is 7 % 5, which is 2.
Answer: It prints
2.
Common mistakes
- Reading
x *= 2 + 3asx * 2 + 3. The whole right side is computed first. - Forgetting integer division with
/=on anintvariable. - Writing
x =+ 3;instead ofx += 3;. The first one compiles but just stores +3 inx, 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++;andtotal += value;are the standard ways to update counters and totals inside a loop.
Connected topics
Videos
Check yourself
4 questions on 1.6 Compound Assignment Operators. Pick an answer to see if you got it, and why.
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?
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?
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?
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