Skip to main content
United StatesComputer ScienceSyllabus dot point

How do the compound assignment operators and the increment and decrement operators provide a shorthand for updating a variable?

Topic 1.4 Compound Assignment Operators: use the compound assignment operators (+=, -=, *=, /=, %=) and the increment and decrement operators (++, --) as shorthand to update variables.

A focused answer to AP CSA Topic 1.4, covering the compound assignment operators, the increment and decrement operators, how each rewrites a longer assignment, and the integer-division traps they can hide, with a traced worked example.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Compound assignment operators
  3. Increment and decrement operators
  4. Watch the type
  5. Try this

What this topic is asking

The College Board (Topic 1.4) wants you to use the compound assignment operators (+=, -=, *=, /=, %=) and the increment and decrement operators (++, --) as a shorthand for updating a variable, and to know exactly what each one is equivalent to. These appear constantly inside loops in later units, so getting the rewrite right now pays off everywhere.

Compound assignment operators

Each operator rewrites to a longer assignment:

  • 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 variable on the left is read and then updated, so it must already hold a value. These operators do not introduce any new arithmetic - they are purely a shorthand - which means every rule from Topic 1.3 still applies, including integer-division truncation.

A useful way to read any compound statement is to mentally rewrite it in its long form before evaluating. count *= 3 becomes count = count * 3, so you read the current value of count, multiply it by 3, and store the result back. This rewrite is exactly how the AP exam expects you to reason: the right-hand side (built from the variable's current value and the operand) is evaluated first, and only then is the result stored back in the variable. Because of that, the right operand can itself be an expression: total += a * b means total = total + (a * b), with the multiplication done first. The shorthand never changes precedence; it just saves you from writing the variable name twice.

Increment and decrement operators

On the AP exam these almost always appear as standalone statements (for example to advance a counter in a loop), where x++ and ++x have the same effect: both increase x by 1. The pre/post distinction (whether the old or new value is used in a surrounding expression) is not the focus; treat count++; simply as "add one to count".

Increment and decrement are the workhorses of iteration in later units. A loop counter is almost always advanced with i++, and a countdown with i--. Recognizing i++ as "add one to i" lets you trace a loop quickly: each pass through the loop runs the body and then bumps the counter by one. Like the compound operators, ++ and -- read and update the same variable, so the variable must already hold a value; you cannot increment a brand-new, unassigned variable.

Watch the type

Because compound operators reuse the variable's own type, they keep integer behavior. If x is an int, then x /= 2 performs integer division and truncates, and x %= 3 gives an int remainder. The compound form does not magically produce a double. This is a favorite exam trap: x *= 1.5 on an int x is actually allowed (Java applies an implicit cast back to int), and it truncates the result, which surprises students.

Try this

Q1. Rewrite score = score + bonus; using a compound assignment operator. [1 point]

  • Cue. score += bonus;

Q2. Given int k = 9;, state the value of k after k /= 2;. [1 point]

  • Cue. 4, because 9 / 2 is integer division, which truncates from 4.5 to 4.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. What is the value of `x` after the following code segment runs? ```java int x = 12; x += 3; x *= 2; x -= 5; x /= 4; ``` (A) `5` (B) `6` (C) `7` (D) `25` (E) `6.25`
Show worked answer →

The answer is (B).

Apply each compound operator in order. x += 3 makes x equal 15. x *= 2 makes x equal 30. x -= 5 makes x equal 25. x /= 4 is 25 / 4, integer division, which truncates to 6. (E) wrongly keeps a decimal, but x is an int, so the result is 6, not 6.25. (A), (C) and (D) misapply one of the steps.

Markers reward processing the operators in sequence and remembering that /= on an int still truncates.

AP 2023 (style)4 marksFree response (code writing). A variable `int total` already holds some value. Write a code segment that, without using the `+` operator in a plain assignment, increases `total` by the value of an `int` variable `bonus`, then doubles `total`, then increases `total` by 1 using the increment operator. Use compound assignment and increment operators only for these updates.
Show worked answer →

A 4-point question testing the compound and increment operators.

total += bonus;
total *= 2;
total++;

Point 1: total += bonus; adds bonus to total (equivalent to total = total + bonus;). Point 2: total *= 2; doubles total (equivalent to total = total * 2;). Point 3: total++; increases total by 1. Point 4: using the required shorthand operators (not a plain total = total + ...). Order matters, since doubling before or after the increment gives different results.

Related dot points

Sources & how we know this