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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 += ymeansx = x + yx -= ymeansx = x - yx *= ymeansx = x * yx /= ymeansx = x / yx %= ymeansx = 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, because9 / 2is integer division, which truncates from4.5to4.
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
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
- Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
- Topic 1.5 Casting and Ranges of Variables: use casting to convert between int and double, predict the effect of truncation, and recognize that an int has a finite range that can overflow.
A focused answer to AP CSA Topic 1.5, covering explicit casting between int and double, the truncation that casting to int causes, where the cast applies in an expression, the finite range of an int, and integer overflow, with a traced worked example.
- Topic 1.1 Why Programming? Why Java?: explain how a program is a sequence of statements executed in order, how Java source is compiled and run, and how to display output and handle simple runtime behavior.
A focused answer to AP CSA Topic 1.1, covering what a program is, the structure of a Java class with a main method, sequential execution, System.out output, and compile-time versus run-time errors, with a fully traced worked program.
- Topic 2.4 Calling a Void Method with Parameters: call a void method that takes parameters, passing arguments that match the parameter list in number, order and type, and explain how arguments are copied to parameters.
A focused answer to AP CSA Topic 2.4, covering parameters versus arguments, matching the argument list in number, order and type, pass-by-value for primitives, overloaded methods, and how the call supplies data to the method, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)