AP® Computer Science A · Unit 1 · Topic 1.6

Compound Assignment and Increment Operators

Compound assignment operators like += pack "take the current value, do something to it, store it back" into one symbol. Increment operators like ++ do the same for adding or subtracting 1, but where you put them changes when the increment happens.

Writing count = count + 1 means: read count, add 1, store the result back in count. That is three jobs for one idea, so Java gives you count += 1 as shorthand. Any compound operator (+=, -=, *=, /=, %=) expands the same way: x *= 3 means x = x·3. Java always finishes computing the right side completely, using whatever value x held the instant before the line ran, then stores the new result. Order matters when you chain several lines: each line uses the value left behind by the line before it, so x += 5; x -= 3; x *= 2; applies those steps one at a time, not all at once.

Increment and decrement operators (++, --) are the same shorthand for adding or subtracting exactly 1, but they come in two flavors depending on where you put the operator. Postfix (x++) hands back the current value first, then increments x afterward. Prefix (++x) increments x first, then hands back the new value. Both leave x one higher once the statement ends — the difference only shows up if you use the result in the same statement, like inside a println or another expression.

This matters because Unit 1 is building your sense that every expression collapses to a value before anything gets stored, and these operators are where that collapsing has a visible order. Once you can trace a line like i++ + ++i step by step, you are ready for loops in later units, where ++ and += are the standard way to move a counter forward each pass.

Key terms

Compound assignment
An operator like += or *= that combines reading, computing, and storing back into x in one step.
Postfix increment (x++)
Returns the current value of x for use in the expression, then increments x by 1 afterward.
Prefix increment (++x)
Increments x by 1 first, then returns the new value for use in the expression.
Expression evaluation order
The fixed sequence Java follows to compute a line fully before storing any result.

Watch it explained

  1. How it worksHow does x *= 3 expand into a full assignment before anything gets stored?
  2. Worked exampleTrace int x = 10; x += 5; x -= 3; x *= 2; x /= 4; and find the value at every step.
  3. How it worksHow does x++ differ from ++x when the value is used inside the same statement?
  4. Worked exampleFind what int i = 3; System.out.println(i++ + ++i); prints.
  5. Common mistakeWhy doesn't x++ change the value that the surrounding expression already grabbed?

Check yourself

Your turn1 of 2
int x = 7; x += 7; x -= 8; x *= 3; What is x now?

End of topic.