콘텐츠로 이동

Unit 1.6: Compound Assignment Operators

Scope: CS Awesome 2, Section 1.6

Learning Goals

By the end of this lesson, you should be able to:

  1. Rewrite ordinary assignments with compound operators.
  2. Use ++ and -- for one-step changes.
  3. Trace a sequence of updates accurately.
  4. Preserve integer arithmetic rules inside updates.

Operation and Assignment in One Step

A compound assignment operator performs an arithmetic operation using a variable's current value and then stores the result back into that variable.

Full assignment Compound form
x = x + 4; x += 4;
x = x - 4; x -= 4;
x = x * 4; x *= 4;
x = x / 4; x /= 4;
x = x % 4; x %= 4;

The arithmetic operator comes before the equal sign.

int score = 10;
score += 5;  // 15
score *= 2;  // 30
score -= 8;  // 22
score /= 4;  // 5 because score is int
score %= 3;  // 2

The Entire Right Side Is Used

int total = 3;
int bonus = 4;
total *= bonus + 2;

This means:

total = total * (bonus + 2); // 18

It does not mean (total * bonus) + 2.

Increment and Decrement

Adding or subtracting one is so common that Java provides two shorter operators.

count++; // count = count + 1;
count--; // count = count - 1;

For AP CSA, use these as standalone updates. Although prefix and postfix forms can act differently inside a larger expression, the exam does not require tricky expression use.

int passengers = 20;
passengers++;
System.out.println(passengers); // 21
passengers--;
System.out.println(passengers); // 20

Arithmetic Rules Still Apply

The variable's type still controls the operation.

int fuel = 9;
fuel /= 2; // 4, not 4.5

double distance = 9;
distance /= 2; // 4.5

The right-side expression is evaluated before the update.

int score = 12;
int penalty = 5;
score -= penalty / 2; // subtracts 2, so score becomes 10

Code Tracing

int x = 2;
int y = 7;
x++;
y -= x;
x *= 2;
y %= 3;
Statement x y
initialized 2 7
x++; 3 7
y -= x; 3 4
x *= 2; 6 4
y %= 3; 6 1

Trace one statement at a time; do not calculate from the original values after a variable has changed.

Practice Missions

Mission 1: Rewrite Both Ways

For each statement, write an equivalent compound assignment. Then convert it back to a full assignment.

credits = credits + 12;
temperature = temperature - 1.5;
pages = pages * 2;
remainder = remainder % 7;

Mission 2: Trace the Battery

Build a trace table for charge and cycles.

int charge = 81;
int cycles = 2;
charge /= 3;
cycles++;
charge += cycles * 4;
charge %= 10;
cycles--;

Mission 3: Scoreboard Updates

Start with int home = 14; and int away = 12;. Use only compound, increment, or decrement operators to:

  1. add 3 to home;
  2. double away;
  3. subtract 2 from home;
  4. increase away by 1.

Print the final scores.

Mission 4: Explain the Surprise

Explain why this code prints 3 rather than 3.5, then change only one declaration so it prints 3.5.

int portions = 7;
portions /= 2;
System.out.println(portions);

Key Summary

Operator Effect
+= Add, then assign.
-= Subtract, then assign.
*= Multiply, then assign.
/= Divide, then assign using the variable's type.
%= Take the remainder, then assign.
++ Increase by one.
-- Decrease by one.

Source scope: CS Awesome 2, Unit 1.6