Lesson 1.6: Compound Assignment Operators | AP CSA

Unit 1 · Lesson 1.6 · Code Mechanics

Lesson 1.6: Compound Assignment Operators

Reading time: 5–7 min · Practice: 6 exercises · Mastery: applied scenario

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
▶ Java Code Editor
Try It Yourself
Write real Java, hit Run, and your code executes on a live compiler. Output is checked automatically.
Tier 2 · AP Practice

Practice Questions

What value is stored in k after the following code segment executes?

int k = 8;
k -= 3;
k *= k;
k /= 5;
Trace each line. Write k's value after each statement before choosing.
A. 1
B. 25
C. 5
D. 4
C. k=8. After 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.
Consider the following code segment.

int num = 5;
num += num;
num *= num;
System.out.println(num);
Carefully evaluate num += num first. What does that give you? Then apply the next operator.
A. 100
B. 25
C. 50
D. 10
A. 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.
Assume 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--;
A. I only
B. II only
C. I and II only
D. I and III only
D. 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.
A student writes the following code intending 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?
A. A compile error because /= requires both sides to be the same type.
B. The program prints 4.5 because total is double, so total / 2 performs double division.
C. The program prints 4.0 because /= always performs integer division when the right side is an int.
D. A run-time error because you cannot divide a double by an int.
B. 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.
What is printed when the following code segment executes?

int j = 3;
j++;
j++;
j--;
j += j;
System.out.println(j);
Trace each operator one at a time. What is j after each line?
A. 8
B. 6
C. 10
D. 4
A. j=3. After 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.
What value does n hold after the following code executes?

int n = 100;
n /= 4;
n %= 7;
n += n;
Trace each line in order. Write n after each statement before choosing.
A. 4
B. 8
C. 25
D. 50
B. n=100. After 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.
What does the program print?
Trace every step before choosing. Write shelf after each line.
A. 0
B. 2
C. 1
D. 4
A. Trace: shelf=20. After -= 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

After 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?
A. shelf += 17;
B. shelf++;
C. shelf *= 2;
D. shelf = shelf + 1;
C. 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.

Extension · Beyond the Exam

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]