Lesson 1.6: Compound Assignment Operators | AP CSA
Lesson 1.6: Compound Assignment Operators
What you'll learn in this lesson
-
1.6.A. Use compound assignment operators
+=,-=,*=,/=, and%=as shorthand for full assignment statements, and predict the value stored after each. -
1.6.A. Use the post-increment (
++) and post-decrement (--) operators to add or subtract 1 from a variable.
This lesson is part of the AP CSA Course and Exam Description (effective May 2027).
AP exam weight: Compound operators appear most often inside loops. The exam tests whether you can trace n += n, n *= 2, and sequences mixing ++ with arithmetic. Prefix form (++x) and operators inside expressions (arr[x++]) are explicitly excluded from the AP exam.
Key Vocabulary
| Term | Definition |
|---|---|
| compound assignment operator | A shorthand combining arithmetic with assignment: +=, -=, *=, /=, %=. |
| post-increment | The ++ operator as a standalone statement; adds 1 to a variable. x++ means x = x + 1. |
| post-decrement | The -- operator as a standalone statement; subtracts 1 from a variable. x-- means x = x - 1. |
Compound assignment operators: shorthand that still assigns
A compound assignment operator combines an arithmetic operation with assignment into one statement. Each one is exactly equivalent to its expanded form.
The Five Operators
x += y means x = x + y
x -= y means x = x - y
x *= y means x = x * y
x /= y means x = x / y
x %= y means x = x % y
The operator performs the arithmetic using the current value of x and the value of y, then stores the result back into x.
Tracing a Sequence
int n = 10; n += 5; // n = 10 + 5 = 15 n *= 2; // n = 15 * 2 = 30 n -= 8; // n = 30 - 8 = 22 n /= 2; // n = 22 / 2 = 11 (int division) n %= 4; // n = 11 % 4 = 3
AP Trap: n += n
n += n is equivalent to n = n + n, which doubles n. Students sometimes read it as “add nothing” or confuse it with n++. If n = 6, then after n += n, n = 12.
Increment and decrement operators
The post-increment operator ++ adds 1 to the stored value of a variable and assigns the result back. The post-decrement operator -- subtracts 1.
Post-form Only on the AP Exam
x++ means x = x + 1 (post-increment)
x-- means x = x - 1 (post-decrement)
AP Exclusion: prefix form (++x) and using either operator inside another expression (e.g., arr[x++]) are out of scope. On the AP exam, x++ always appears as a standalone statement.
Example in Context
int count = 0; count++; // count = 1 count++; // count = 2 count--; // count = 1 System.out.println(count); // prints 1
Practice Questions
k after the following code segment executes?int k = 8; k -= 3; k *= k; k /= 5;
k's value after each statement before choosing.k -= 3: k=5. After k *= k: k=5*5=25. After k /= 5: k=25/5=5 (int division, exact here). A results from 5/5 then subtracting 4. B stops after the multiplication. D uses wrong intermediate values.
int num = 5; num += num; num *= num; System.out.println(num);
num += num first. What does that give you? Then apply the next operator.num += num means num = num + num = 5 + 5 = 10. Then num *= num means num = 10 * 10 = 100. B stops after the first operation. C multiplies 10 by 5. D stops after the first operation without squaring.
int x = 12. Which of the following are equivalent to x -= 4?I.
x = x - 4;II.
x = 4 - x;III.
x--; x--; x--; x--;
x -= 4 subtracts 4, giving 8. Statement I: x = x - 4 = 12 - 4 = 8—identical. Statement II: x = 4 - x = 4 - 12 = -8—wrong sign. Statement III: four decrements each subtract 1, total subtraction of 4, giving 8—equivalent. I and III both produce 8; II produces -8.
total to hold exactly half its current value as a decimal.double total = 9.0; total /= 2; System.out.println(total);
The student expects
4.5. Which of the following best describes the actual output?
/= requires both sides to be the same type.4.5 because total is double, so total / 2 performs double division.4.0 because /= always performs integer division when the right side is an int.double by an int.total /= 2 expands to total = total / 2. Since total is double, this is 9.0 / 2—double divided by int = double = 4.5. The student’s expectation is correct. A is wrong: /= works with mixed types. C is wrong: the type of the left operand determines whether the result is double or int. D is wrong: dividing double by int is valid.
int j = 3; j++; j++; j--; j += j; System.out.println(j);
j after each line?j++: j=4. After j++: j=5. After j--: j=4. After j += j: j=4+4=8. B stops before the last step. C applies an extra increment. D stops after the decrement.
n hold after the following code executes?int n = 100; n /= 4; n %= 7; n += n;
n after each statement before choosing.n /= 4: n=25 (int division exact). After n %= 7: 25 ÷ 7 = 3 remainder 4, so n=4. After n += n: n=4+4=8. A stops after the %= step. C stops after /=. D skips the %= step entirely.
shelf after each line.-= 3: shelf=17. After += shelf: shelf=17+17=34. After /= 5: shelf=34/5=6 (int division). After %= 3: shelf=6%3=0. Prints 0. B and C result from arithmetic errors mid-trace. D results from stopping before the final %=.
Part B: Identify the operator
shelf -= 3 runs with shelf = 20, the next line is shelf += shelf. Which of the following single statements produces the same result as shelf += shelf?shelf += 17;
shelf++;
shelf *= 2;
shelf = shelf + 1;
shelf += shelf means shelf = shelf + shelf, which doubles shelf. shelf *= 2 also doubles shelf and always produces the same result regardless of shelf's value. A uses the hardcoded value 17 so it only works for that specific value. B adds 1. D also adds 1.
Part C: Structured response
A new requirement says the final shelf count must be at least 1 (to avoid an empty shelf). Write a version of the last line (shelf %= 3) that ensures shelf is never 0 after the operation, and explain in two sentences why your change works.
Scoring Rubric (3 points)
-
+1: Writes a correct replacement, such as
shelf = (shelf % 3) + 1;orshelf %= 3; if (shelf == 0) shelf = 3;or another valid approach. -
+1: Explains why: the
% 3operator can produce 0 whenshelfis divisible by 3; adding 1 to the result shifts the range from [0,1,2] to [1,2,3], guaranteeing a minimum of 1. - +1: No credit if the answer changes the modulus value without explanation or uses a constant that only works for the specific input value.
Why post-increment matters in real code
In a loop like for (int i = 0; i < 10; i++) (defined in the Java Language Specification §15.14), the i++ is post-increment used as a standalone update statement—exactly the AP-tested form. The difference between i++ and ++i only matters when the expression’s return value is used (e.g., arr[i++] vs arr[++i]), which is why those forms are excluded from the AP exam. In most real loops, i++ and ++i behave identically.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]