1.6 - Compound Assignment Operators

superdancer16

Introduction

Writing code like count = count + 1 or total = total + price is common in programming, but it's also repetitive. Topic 1.6 introduces compound assignment operators, which provide shorthand notation for these operations. These operators make code more concise while performing the same calculations.

Compound assignment operators and increment/decrement operators are syntactic conveniences that don't add new functionality—they simply provide cleaner ways to write common assignment patterns.

Compound Assignment Operators

Overview

Compound assignment operators (+=, -=, *=, /=, %=) can be used in place of the assignment operator in numeric expressions. A compound assignment operator performs the indicated arithmetic operation between the value on the left and the value on the right, and then assigns the result to the variable on the left.

General form:

variable operator= expression;

This is equivalent to:

variable = variable operator expression;

The Five Compound Operators

Addition assignment (+=)

int x = 10;
x += 5;             // Same as: x = x + 5
// x is now 15

Subtraction assignment (-=)

int balance = 100;
balance -= 25;      // Same as: balance = balance - 25
// balance is now 75

Multiplication assignment (*=)

int score = 10;
score *= 3;         // Same as: score = score * 3
// score is now 30

Division assignment (/=)

int total = 50;
total /= 2;         // Same as: total = total / 2
// total is now 25

Remainder assignment (%=)

int value = 17;
value %= 5;         // Same as: value = value % 5
// value is now 2

Using Compound Operators with Expressions

The right side can be any expression:

int a = 10;
int b = 5;
a += b * 2;         // Same as: a = a + (b * 2)
// a = 10 + 10 = 20

int count = 100;
count -= count / 10;    // Same as: count = count - (count / 10)
// count = 100 - 10 = 90

Important: The right side expression is evaluated first, then the operation is performed.

int x = 8;
x *= x + 2;         // Same as: x = x * (x + 2)
// Right side: x + 2 = 8 + 2 = 10
// Then: x = 8 * 10 = 80

With Different Data Types

Compound operators work with double values as well:

double price = 19.99;
price += 5.00;          // price is now 24.99

double total = 100.0;
total *= 1.08;          // Apply 8% increase: 108.0

double balance = 50.5;
balance /= 2;           // balance is now 25.25

Increment and Decrement Operators

Post-Increment (++)

The post-increment operator ++ is used to add 1 to the stored value of a numeric variable. The new value is assigned to the variable.

int count = 10;
count++;            // Same as: count = count + 1
// count is now 11

Common use cases:

// Counting iterations
int total = 0;
total++;
total++;
total++;
// total is now 3

// Incrementing indices
int index = 0;
index++;            // Move to next position

Post-Decrement (--)

The post-decrement operator -- is used to subtract 1 from the stored value of a numeric variable. The new value is assigned to the variable.

int lives = 3;
lives--;            // Same as: lives = lives - 1
// lives is now 2

Common use cases:

// Counting down
int countdown = 10;
countdown--;
countdown--;
// countdown is now 8

// Decreasing inventory
int stock = 50;
stock--;            // Sell one item

Important Restrictions

Exclusion: The use of increment and decrement operators in prefix form (e.g., ++x) is outside the scope of the AP Computer Science A course and exam.

Exclusion: The use of increment and decrement operators inside other expressions (e.g., arr[x++]) is outside the scope of the AP Computer Science A course and exam.

For the AP exam, you will only see:

// ALLOWED - standalone statements
x++;
count--;

// NOT ON EXAM - prefix form
++x;
--count;

// NOT ON EXAM - within expressions
arr[index++];
total += count--;
int y = x++;

Operator Equivalence Table

Common Mistakes

Mistake 1: Confusing operator order

// These are DIFFERENT
int x = 10;
x += 5 * 2;         // x = x + (5 * 2) = 10 + 10 = 20

int y = 10;
y *= 5 + 2;         // y = y * (5 + 2) = 10 * 7 = 70

Mistake 2: Using prefix increment/decrement

// WRONG - not on AP exam
int x = 5;
++x;                // Prefix form not tested

// CORRECT
int x = 5;
x++;                // Post-increment only

Mistake 3: Using increment/decrement in expressions

// WRONG - not on AP exam
int x = 5;
int y = x++;        // Not tested

// CORRECT - separate statements
int x = 5;
int y = x;
x++;

Mistake 4: Forgetting that compound operators modify the variable

int score = 100;
score += 10;
score += 10;
// score is now 120 (not 110!)
// Each += modifies the current value

Mistake 5: Integer division with /=

int x = 7;
x /= 2;             // Integer division: x = 7 / 2 = 3 (not 3.5!)

double y = 7.0;
y /= 2;             // Floating-point: y = 7.0 / 2 = 3.5

Practice Problems