AP · Computer Science A · Primitive Types and Expressions

Compound Assignment and Increment Operators

11 min readUpdated December 1, 2026FreeAP Computer Science A, Using Objects and Methods

A compound assignment operator updates a variable using its own current value: x += 5; does the same as x = x + 5;, and -=, *=, /= and %= work the same way with their own operators. The statements x++; and x--; add 1 to x and subtract 1 from it. To find a variable's final value, trace the statements in order with a table, because each one starts from the value the last one left.

Quick check

  1. Both operands are int. What is the value of 17 / 5 in Java?Show answerHide answer
    1. int division drops the fractional part of 3.4.
  2. x holds 4 and the statement x = x + 3; runs. What does x hold now?Show answerHide answer
    1. The right side uses the old value, 4, and the result replaces it.

Know these already? Jump to the practice questions

What you'll be able to do

  • State the plain assignment that each of +=, -=, *=, /=, %=, ++ and -- stands for.
  • Determine a variable's value after a sequence of compound assignments, using a trace table.
  • Explain why x *= a + b multiplies x by the whole of a + b.
  • Identify a statement that uses the wrong operator for the update a task needs.

Key terms

Compound assignment operator

An operator such as += that applies an arithmetic operator to a variable and the value on its right, then stores the result back in the variable.

In plain English
A shortcut for "update this variable using its current value".
Don't write
"x += 2 adds 2 to x without changing x."

Increment and decrement operators

++ adds 1 to a variable and -- subtracts 1; the statement n++; does the same as n += 1;.

In plain English
Count up by one, or count down by one.
Don't write
"n++ adds 2 to n, one for each plus sign."

Trace table

A table with one column per variable and one row per statement, showing each value after that statement runs.

In plain English
A record of every change, line by line.
Don't write
"Only the final value needs tracing."

What does a compound assignment operator do?

Each operator stands for a plain assignment. Starting each time from x = 17:

StatementSame asx afterwards
x += 3;x = x + 3;20
x -= 3;x = x - 3;14
x *= 3;x = x * 3;51
x /= 3;x = x / 3;5
x %= 3;x = x % 3;2

If x is an int, /= is int division, so 17 / 3 truncates to 5, and %= keeps the remainder, 2. If x is a double, /= keeps the fraction.

The whole right side is worked out first. x *= 2 + 3; means x = x * (2 + 3);, so with x at 4 the result is 20, not 4 * 2 + 3 = 11.

Learn this: these operators are Java syntax, not on the Java Quick Reference.

Key idea

x op= expression means x = x op (expression), with the result converted back to the type of x: the right side is evaluated in full before it meets x.

Check yourself

An int variable n holds 9, and n /= 2; runs. What does n hold?
Show answerHide answer
  1. n = n / 2 is int division, and 4.5 truncates to 4.

What do ++ and -- do?

count++; adds 1 to count, and count--; subtracts 1. They are the shortest way to write count += 1; and count -= 1;, and the loops of Unit 2 use them to count.

int lives = 3;
lives--;
lives--;
lives++;

lives goes 3, 2, 1, 2.

Here ++ and -- are always used as statements on their own, such as lives--;. Each one changes the variable by exactly 1, whatever its type: a double holding 2.5 becomes 3.5 after ++.

Check yourself

After int c = 0; c++; c++; c--; what does c hold?
Show answerHide answer
  1. The value goes 0, 1, 2, 1.

How do you trace a sequence of updates?

Make one column per variable and one row per statement. For each row, copy the values from the row above and change only the variable on the left of that statement.

int a = 6;
int b = 4;
a += b;
b *= 3;
a -= b;
b %= 5;
a++;
Statementab
start64
a += b;104
b *= 3;1012
a -= b;-212
b %= 5;-22
a++;-12

Row 4 uses the new b, 12, not the starting 4. Reading the row above every time is what stops you using a stale value.

Check yourself

In the trace, what would a hold after a -= b; if b had wrongly been read as 4?
Show answerHide answer

6, because 10 − 4 = 6. The correct value is −2, using the updated b of 12.

Worked example 1: Trace one variable through every operator

Question: What is printed when this code segment runs?

int score = 10;
score += 7;
score *= 2;
score /= 4;
score %= 3;
score--;
System.out.println(score);

What tells you the method

A chain of updates to one variable: trace it with one row per statement.

  1. Step 1: Trace each statement.

    Statementscore
    start10
    score += 7;17
    score *= 2;34
    score /= 4;8
    score %= 3;2
    score--;1
    Why

    score is an int, so 34 / 4 truncates 8.5 to 8, and 8 % 3 leaves 2.

  2. Step 2: Print the final value.

    println prints 1.

    Why

    Only the value after the last update is printed.

Check it

Re-trace with plain assignments: 10 + 7 = 17, 17 × 2 = 34, 34 / 4 = 8, 8 % 3 = 2 and 2 − 1 = 1.

Answer

1

Worked example 2: Trace two variables of different types

Question: What is printed when this code segment runs?

double total = 20.0;
int items = 3;
total -= 5;
items++;
total /= items;
items *= items;
System.out.println(total);
System.out.println(items);

What tells you the method

Two variables of different types: trace both columns, and note which divisions are double.

  1. Step 1: Trace the statements.

    Statementtotalitems
    start20.03
    total -= 5;15.03
    items++;15.04
    total /= items;3.754
    items *= items;3.7516
    Why

    total is a double, so 15.0 / 4 keeps the fraction; items *= items squares 4.

  2. Step 2: Print both.

    3.75, then 16.

    Why

    The division used the updated items, 4, from the row above.

Check it

3.75 × 4 = 15.0, which matches total before the division.

Answer

3.75 then 16, one per line.

Worked example 3: Evaluate the right side first

Question: int x = 5; and int y = 2; are declared. What value does x hold after x *= y + 1; runs?

  • A) 8
  • B) 10
  • C) 11
  • D) 15

What tells you the method

A compound operator with an expression on the right: rewrite it as a plain assignment with parentheses.

  1. Step 1: Rewrite the statement.

    x *= y + 1; means x = x * (y + 1);.

    Why

    A compound assignment evaluates the whole right side before applying its operator.

  2. Step 2: Evaluate.

    y + 1 is 3, and 5 × 3 = 15.

    Why

    The parentheses come from the operator's meaning, not from precedence in the original line.

Check it

Each distractor comes from one slip: 8 treats *= as +=, 10 drops the + 1, and 11 computes x * y + 1.

Answer

D

Your turn

Question: What is printed when this code segment runs?

int n = 50;
n -= 8;
n /= 5;
n %= 3;
n++;
System.out.println(n);
  1. Step 1: First two updates.

    50 − 8 = 42, then 42 / 5 is 8 in int division.

  2. Step 2: Your go: finish the trace and give the printed value.

Show the full solutionHide the solution

Step 2. 8 % 3 is 2, because 8 = 2 × 3 + 2. Then n++ makes it 3.

Answer: 3

Traps

Trap

x *= a + b; multiplies x by a and then adds b.

It seems right because * normally comes before +. But a compound assignment evaluates its whole right side first, so the statement means x = x * (a + b);.

m holds 3. What does m hold after m *= 4 + 1;?

15, because 3 × (4 + 1) = 15.

Trap

t /= 2; on an int keeps the half.

It seems right because "divide by 2" sounds exact. But for an int variable it is int division, so 7 becomes 3.

An int t holds 7. What does it hold after t /= 2;?
  1. 7 / 2 truncates 3.5 to 3.

Trap

x =+ 3; adds 3 to x.

It seems right because it uses the same two symbols as +=. But the order matters: =+ 3 is a plain assignment of the value +3, so x becomes 3 whatever it held before.

x holds 10. What does it hold after x =+ 3;?
  1. The statement assigns +3; it does not add.

What's examined

On the AP® Computer Science A exam, Unit 1 compound assignment appears in multiple-choice questions that ask for a variable's value or the printed output after a sequence of statements, and += and ++ reappear in the loops of Unit 2. A trace table is the reliable method.

Practice questions

  1. Recall Which statement has the same effect as total = total * 4;?
  • A) total *= 4;
  • B) total =* 4;
  • C) total += 4;
  • D) total * 4;
Show answer

Answer: A. *= multiplies total by 4 and stores the result back.

Why the others are wrong:

  • B has the symbols in the wrong order and does not compile.
  • C uses the wrong operator, so it adds 4.
  • D computes a value but stores it nowhere, so it does not compile as a statement.
  1. Apply What is printed when this code segment runs?
int k = 9;
k += 6;
k %= 4;
k *= 10;
k--;
System.out.println(k);
Show answer

Answer: 29

Marking points:

  • k goes 9, 15, 3, 30, 29.
  • 15 % 4 is 3, because 15 = 3 × 4 + 3.
  1. Apply What value does p hold after this code segment runs?
int p = 3;
p *= p;
p += p;
Show answer

Answer: 18

Marking points:

  • p *= p gives 3 × 3 = 9.
  • p += p gives 9 + 9 = 18.
  1. Exam-style What value does x hold after this code segment runs?
int x = 20;
x -= 2 * 3;
x /= 4;
x++;
  • A) 3
  • B) 4
  • C) 4.5
  • D) 14
Show answer

Answer: B. 20 − 6 = 14, then 14 / 4 = 3 in int division, then 3 + 1 = 4.

Why the others are wrong:

  • A forgets the final x++.
  • C uses true division, 3.5, which an int variable cannot hold.
  • D computes (x - 2) * 3 first, giving 54, then 13, then 14.
  1. Exam-style This code segment is meant to split 200 seconds into whole minutes and leftover seconds, printing 3 and then 20. Give the number of the line that stops it working.
1: int seconds = 200;
2: int minutes = seconds;
3: minutes /= 60;
4: seconds /= 60;
5: System.out.println(minutes);
6: System.out.println(seconds);
Show answer

Answer: Line 4.

Marking points:

  • Line 3 is correct: 200 / 60 is 3.
  • Line 4 divides again, so seconds becomes 3 and the code prints 3 twice.
  • Changing line 4 to seconds %= 60; leaves the remainder, 20.
  1. Exam-style What values do a and b hold after this code segment runs?
int a = 4;
int b = 7;
a += b;
b -= a;
a *= b;
  • A) a is 44 and b is 4
  • B) a is 11 and b is -4
  • C) a is -44 and b is -4
  • D) a is 28 and b is 7
Show answer

Answer: C. a becomes 11, then b becomes 7 − 11 = −4, then a becomes 11 × −4 = −44.

Why the others are wrong:

  • A subtracts the wrong way round, 11 − 7, in the second update.
  • B stops before the last statement.
  • D uses the starting values in every statement.
  1. Recall What is the name of the operator -- in the statement count--;?
Show answer

Answer: The decrement operator.

Marking points:

  • It subtracts 1 from count, the same as count -= 1;.

Key points

Recall, then reveal

Say the main points from memory first, then check them.

Show key pointsHide key points
  • x op= e means x = x op (e), converted back to the type of x, with the whole of e worked out first.
  • /= and %= on an int variable use int division and the remainder.
  • x++; and x--; change x by exactly 1.
  • Trace with one row per statement, always reading the values from the row above.
  • =+ is not +=: it assigns a positive value instead of adding.

Keep learning

Explore AP Computer Science A tutoring →

View the full Computer Science A syllabus →

More in this unit

Using Objects and Methods

All Primitive Types and Expressions notes

Part of Novark's AP Computer Science A notes