Fiveable

💻AP Computer Science A Unit 1 Review

QR code for AP Computer Science A practice questions

1.6 Compound Assignment Operators

1.6 Compound Assignment Operators

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

TLDR

Compound assignment operators (+=, -=, *=, /=, %=) combine a math operation with assignment in one step, so score += 10 does the same thing as score = score + 10. The post-increment (x++) and post-decrement (x--) operators add or subtract 1 from a numeric variable, and you will see them constantly as loop counters. Your main job is to trace these statements and figure out the exact value stored in a variable afterward.

Why This Matters for the AP Computer Science A Exam

This topic builds your skill at reading code and predicting the exact value a variable holds after each statement runs. That kind of careful tracing shows up all over the multiple-choice section, where questions often chain several compound assignments together and ask for the final result.

These operators also appear throughout free-response code writing. Loops almost always update a counter with i++, and you will use += to build up running totals when working with numbers, strings, arrays, and ArrayLists later in the course. Getting comfortable with how the value changes after each line now makes those later questions much easier.

Key Takeaways

  • x += y is shorthand for x = x + y, and the same pattern holds for -=, *=, /=, and %=: do the operation between left and right, then store the result back in the left variable.
  • The variable must already be initialized before you use a compound assignment, because the operation needs the current value.
  • x++ adds 1 and x-- subtracts 1, and both store the new value back in the variable. The AP exam only uses the postfix form (x++, not ++x) and only as a standalone statement.
  • The right side is fully evaluated first, then the operation is applied. In x *= 2 + 3, the 2 + 3 happens first, so x is multiplied by 5.
  • Integer rules still apply: /= between two int values does integer division, and %= gives the remainder.
  • When you trace these in MCQs, write down the value after each line instead of doing it all in your head.

How Compound Assignment Operators Work

Math and Assignment Combined

The five compound assignment operators (+=, -=, *=, /=, %=) perform an operation and an assignment in one step. When you write total += price, it is exactly equivalent to total = total + price. The operator takes the current value, performs the operation with the right side, and stores the result back in the variable on the left.

These operators work with numeric variables (int and double in this course). Because they use the current value, the variable must already exist and hold a value first. You cannot use a compound assignment to declare a brand new variable.

What makes these operators handy is that they clearly show you are updating a value based on its current state. Code like balance -= payment immediately tells you that you are reducing balance by payment.

The += Operator

The += operator is the one you will see most because building up a total is such a common pattern. Whether you are summing numbers, tracking a score, or collecting points, += does the job: sum += num adds num to sum.

+= also works with strings for concatenation, which you will use more in Topic 1.15. Writing message += "!" appends an exclamation point. With strings, += produces a new String object because strings are immutable, while with numbers it simply updates the stored value.

Increment and Decrement

The ++ operator adds 1 to a variable, and -- subtracts 1. On the AP exam you will only see the postfix form: count++ or count--. These are equivalent to count += 1 and count -= 1.

These operators work on numeric variables, but you will almost always see them on int loop counters, which is exactly what counting needs. The prefix form (++x) and using these operators inside other expressions (like arr[x++]) are outside the scope of this course, so on the exam they show up alone as their own statement.

Order of Operations

The right side of a compound assignment is fully evaluated before the operation is performed. In x *= 2 + 3, the addition happens first (giving 5), then x is multiplied by 5. If you have total += calculateTax(price), the method runs first, then its return value is added to total.

This makes tracing predictable: everything to the right of the operator is calculated first, then the operation updates the variable.

Code Examples

Basic compound assignment operators in action:

</>Java
// Example: Basic compound assignment operators
public class ScoreTracker {
    public static void main(String[] args) {
        // Initialize variables
        int score = 100;
        double average = 85.5;
        int lives = 3;

        // Using += to add points
        score += 50;  // score is now 150
        System.out.println("Score after bonus: " + score);

        // Using -= to subtract
        score -= 25;  // score is now 125
        System.out.println("Score after penalty: " + score);

        // Using *= to multiply
        score *= 2;   // score is now 250
        System.out.println("Score after double multiplier: " + score);

        // Using /= to divide
        score /= 5;   // score is now 50
        System.out.println("Score after division: " + score);

        // Using %= for remainder
        score %= 15;  // score is now 5 (50 % 15)
        System.out.println("Score modulo 15: " + score);

        // Compound assignment with doubles
        average += 4.5;  // average is now 90.0
        average *= 1.1;  // 10% increase, average is now 99.0
        System.out.println("New average: " + average);
    }
}

Increment and decrement:

</>Java
// Example: Increment and decrement operators
public class Counter {
    public static void main(String[] args) {
        int count = 0;

        // Post-increment - adds 1
        count++;  // count is now 1
        System.out.println("After count++: " + count);

        count++;  // count is now 2
        count++;  // count is now 3
        System.out.println("After three increments: " + count);

        // Post-decrement - subtracts 1
        count--;  // count is now 2
        System.out.println("After count--: " + count);

        // Common loop usage
        System.out.println("Counting up:");
        for (int i = 0; i < 5; i++) {
            System.out.println("i = " + i);
        }

        // Using increment in while loop
        int x = 0;
        while (x < 3) {
            System.out.println("x = " + x);
            x++;  // increment at end of loop
        }
    }
}

String concatenation with +=:

</>Java
// Example: String concatenation with +=
public class MessageBuilder {
    public static void main(String[] args) {
        String message = "Hello";

        // Build up a message
        message += " ";      // message is "Hello "
        message += "World";  // message is "Hello World"
        message += "!";      // message is "Hello World!"

        System.out.println(message);

        // Building a string in a loop
        String result = "";
        for (int i = 1; i <= 5; i++) {
            result += i;     // concatenates numbers as strings
            if (i < 5) {
                result += ", ";
            }
        }
        System.out.println("Numbers: " + result);  // "1, 2, 3, 4, 5"

        // Mixed types with +=
        String info = "Score: ";
        int points = 150;
        info += points;  // automatic conversion to string
        System.out.println(info);  // "Score: 150"
    }
}

Common Errors and Debugging

Using Compound Assignment Before Initialization

The variable must exist and have a value before you use a compound assignment:

</>Java
// ERROR: Variable not initialized
int total;
total += 10;  // Compiler error: variable total might not have been initialized

// CORRECT: Initialize first
int total = 0;
total += 10;  // Now total is 10

This error happens because compound assignment uses the current value. No current value means the operation cannot run.

Type Behavior with Compound Assignment

Watch how types interact:

</>Java
int score = 100;
score += 10.5;  // Result is 110, since the result is stored back in an int

double precise = 100.0;
precise += 10;  // Works: the int is promoted to double

int count = 5;
count++;        // OK on an int counter

Compound assignments store the result back in the left variable's type, so adding a double to an int variable still leaves an int value.

Forgetting That Operators Modify the Variable

A common mistake is forgetting these operators change the variable itself:

</>Java
int x = 5;
int y = x;
x += 3;  // x is now 8
System.out.println("x = " + x);  // 8
System.out.println("y = " + y);  // Still 5!

// The operators modify only the variable they're applied to

Copying x into y does not link them. Updating x later leaves y unchanged.

Practice Problems

Problem 1: What does this code print?

</>Java
int a = 10;
int b = 3;
a += b;
b *= 2;
a -= b;
System.out.println("a = " + a + ", b = " + b);

Solution:

  • Initial: a = 10, b = 3
  • a += b: a = 13, b = 3
  • b *= 2: a = 13, b = 6
  • a -= b: a = 7, b = 6
  • Prints: "a = 7, b = 6"

Problem 2: Build a string that displays a countdown:

</>Java
// Complete this method
public static String countdown(int start) {
    String result = "Countdown: ";
    // Your code here
    return result;
}
// countdown(5) should return "Countdown: 5 4 3 2 1 Blast off!"

Solution:

</>Java
public static String countdown(int start) {
    String result = "Countdown: ";
    for (int i = start; i > 0; i--) {
        result += i + " ";
    }
    result += "Blast off!";
    return result;
}

Problem 3: Fix this code that is trying to calculate compound interest:

</>Java
double balance = 1000;
double rate = 0.05;  // 5% interest
int years = 3;

for (int i = 0; i < years; i++) {
    balance + balance * rate;  // ERROR: This line is wrong
}
System.out.println("Final balance: " + balance);

Solution: Change the error line to use compound assignment:

</>Java
balance += balance * rate;  // Adds interest to balance
// Or equivalently:
balance *= (1 + rate);      // Multiplies balance by 1.05

How to Use This on the AP Computer Science A Exam

Code Tracing

When you see compound assignments in a multiple-choice question, work line by line and write the value of each variable after every statement. Chained operations like the Problem 1 example are easy to get right on paper but easy to slip on in your head. Slow down and track every change.

Free Response

Loops in free-response code writing almost always update a counter with i++. You will also reach for += to build running totals, like summing values as you traverse an array or ArrayList later in the course. Using these operators correctly keeps your loops clean and your accumulation logic clear.

Common Trap

Remember the exam only uses postfix x++ and x--, never the prefix ++x, and never embedded inside another expression. If a problem shows these operators, they appear as their own statement. Also keep integer division in mind: /= between two int values drops the fractional part.

Common Misconceptions

  • Some students think x++ and ++x behave differently on the exam. The prefix form is not part of this course, so you only need the postfix x++ and x--, used as standalone statements.
  • += does not only work with numbers. It also concatenates strings, producing a new String object each time because strings are immutable.
  • Compound assignment does not skip the right-side math. The entire right side is evaluated first, then the operation updates the variable, so x *= 2 + 3 multiplies x by 5.
  • These operators change the variable they are applied to. Copying a value into another variable first does not keep the two in sync afterward.
  • A compound assignment cannot initialize a variable. The variable must already hold a value, since the operation needs that current value to work.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

assignment statement

A statement that stores a value in a variable using the assignment operator or a compound assignment operator.

compound assignment operator

Operators (+=, -=, *=, /=, %) that perform an arithmetic operation and assign the result to a variable in a single statement.

post-decrement operator

The -- operator used after a variable to subtract 1 from its stored value and assign the new value to the variable.

post-increment operator

The ++ operator used after a variable to add 1 to its stored value and assign the new value to the variable.

Frequently Asked Questions

How do I write compound assignment operators in Java?

Use the shorthand operators just like the CED says: they do the operation on the right with the current value on the left, then store the result back in the left variable. Examples: - x += 5; // same as x = x + 5; - y -= 2; // y = y - 2; - n *= 3; // n = n * 3; - d /= 4; // d = d / 4; (watch integer division if d is an int) - r %= 7; // r = r % 7; For adding or subtracting exactly 1 use post-increment/post-decrement: - i++; // add 1 and store back - i--; // subtract 1 and store back Important AP details from the CED: compound assignments can do implicit narrowing (e.g., short s; s += 1; is allowed), and the AP scope excludes prefix ++/-- and using ++/-- inside other expressions (like arr[x++])—don’t use those on the exam. Review integer division and modulo behavior too. Want practice? Use the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and over 1,000 AP CS A practice problems (https://library.fiveable.me/practice/ap-computer-science-a).

What's the difference between `+=` and just using `+` and `=`?

+= is a shorthand that does the operation and assignment in one step: x += y is equivalent to x = x + y in result for most cases, but there are two important differences you should know for AP CSA (EK 1.6.A): - Type narrowing: compound assignment does an implicit cast when the left variable has a smaller type. Example: short s = 1; s += 2; // compiles but short s2 = 1; s2 = s2 + 2; // won’t compile without (short) cast because s2+2 is int. - Readability and side effects: x += f() evaluates f() once (same as x = x + f()), but be careful if x appears inside f()—that can change behavior. Also post-increment rules (x++ ) are separate EKs. For quick AP review and examples, see the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL). For extra practice problems, Fiveable’s AP CSA practice set is useful (https://library.fiveable.me/practice/ap-computer-science-a).

Why does `x += 5` give me a different result than `x = x + 5` sometimes?

Most of the time x += 5 and x = x + 5 do the same math, but they differ when the left-hand variable is a narrower primitive type (byte, short, char). In Java x + 5 is evaluated using int arithmetic, so the result is an int. A plain assignment x = x + 5 then needs an explicit cast back to the narrower type, or it won’t compile. But the compound operator (x += 5) does the operation and then implicitly narrows the result back to x’s type for you. Examples: - byte b = 10; b += 5; // OK (implicit narrowing) - byte b = 10; b = b + 5; // compile error: int cannot be assigned to byte without cast - byte b = 10; b = (byte)(b + 5); // works This is the “implicit narrowing conversion” behavior listed in the CED for Topic 1.6. For more examples and practice, check the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1). For lots of practice problems, see (https://library.fiveable.me/practice/ap-computer-science-a).

What's the syntax for using `*=` and `/=` operators?

Compound assignment combines an arithmetic op with =. Syntax: - x *= y; // same as x = (type of x)(x * y) - x /= y; // same as x = (type of x)(x / y) Examples: - int a = 5; a *= 3; // a becomes 15 - double d = 10.0; d /= 4; // d becomes 2.5 - int b = 7; b /= 2; // b becomes 3 (integer division) Two AP-specific gotchas: - Integer division rules still apply: dividing two ints truncates toward zero (7/2 -> 3). If you want a fractional result, make one operand double. - Compound assignment performs an implicit narrowing cast to the left-hand variable’s type. For example, short s = 10; s *= 2.5; is allowed because the result is implicitly cast back to short. These operators are covered in EK 1.6.A.1 (compound assignment) and you’ll see them on Unit 1 materials (unit overview: https://library.fiveable.me/ap-computer-science-a/unit-1). For a short topic guide, check the study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL).

I'm confused about when to use `++` vs `+= 1` - are they the same thing?

Short answer: usually yes—but watch context. If you use the increment as a standalone statement, x++; and x += 1; (and x = x + 1;) do the same thing for Java numeric primitives: they add 1 and store the new value. Example: int x = 5; x++; // x is 6 Differences to remember (from the CED keywords EK 1.6.A.2 and EK 1.6.A.1): - The post-increment operator ++ is defined to add 1 to the stored value. When used by itself they’re equivalent to += 1. - Using ++ inside other expressions (e.g., arr[i++] or y = x++) is outside AP CSA scope—don’t rely on that on the exam. - Compound assignments (+=, -=, etc.) can perform implicit narrowing conversion in Java (e.g., byte b = 1; b += 1; compiles while b = b + 1; needs a cast). So += can be more permissive with smaller types. For more practice and a quick refresher see the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1).

Can someone explain what `%=` does with an example?

The %= operator is a compound assignment that takes the remainder after division and stores it back in the left variable. It’s equivalent to x = x % y but evaluated only once on the left-hand variable (and may do an implicit narrowing conversion in Java). Example: int x = 17; x %= 5; // same as x = x % 5; System.out.println(x); // prints 2 because 17 divided by 5 leaves remainder 2 Use %= whenever you want to keep the remainder (useful for cycles, parity checks, or wrapping indices). On the AP CSA exam Topic 1.6, you should be able to read/write these and predict the stored value (LO 1.6.A, EK 1.6.A.1). For a quick refresher, see the compound assignment study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-a).

What's the difference between `x++` and `++x` and why can't I use `++x` on the AP exam?

x++ is the post-increment: it returns the current value, then adds 1 to the variable. ++x is the pre-increment: it adds 1 first, then returns the new value. Example: int x = 5; int a = x++; // a==5, x==6 int b = ++x; // b==7, x==7 On the AP CSA exam you should NOT use ++x (prefix) or use increments inside other expressions (like arr[x++])—the CED explicitly excludes prefix increment/decrement and usage inside expressions (see EK 1.6.A.2 and the exclusion statement). Stick to allowed forms: x++; x--; or compound assignments (x += 1, x -= 1, x = x + 1) used as standalone assignment statements. That avoids confusing evaluation-order/side-effect issues the exam won’t test. For extra practice on compound assignment and post-increment rules, check the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL), the Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1), and many practice questions (https://library.fiveable.me/practice/ap-computer-science-a).

How do I trace through code that uses compound assignment operators step by step?

Think of tracing compound assignments as three steps: rewrite, evaluate, assign. 1) Rewrite the compound form into the equivalent full assignment (EK 1.6.A.1): x += y → x = x + y x *= y → x = x * y, etc. 2) Evaluate the right-hand expression using current values (watch integer division and % behavior). If types differ, apply implicit narrowing/conversion rules before assignment. 3) Assign the computed result back to the left variable (this is the stored value you report). For post-increment/decrement (x++ / x--) remember they change the variable by ±1 but return the old value—and per the CED you only need to handle post-form when it’s a standalone statement (EK 1.6.A.2). Quick examples: - int a = 7; a /= 2; → rewrite a = a / 2; evaluate 7/2 = 3 (integer division); assign a = 3. - int b = 5; b %= 4; → b = b % 4 → 5 % 4 = 1 → b = 1. - int c = 4; c += 2 * 3; → c = c + (2*3) → c = 4 + 6 = 10. On the exam you may be asked to determine the stored value after a statement—show these steps (rewrite → evaluate RHS → apply type rules → assign). For a quick topic review check Fiveable’s study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-a) to solidify tracing skill.

Why am I getting weird results when I use `x++` inside an array like `arr[x++]`?

When you write arr[x++], Java uses the current value of x as the index, then increments x as a side effect. That’s fine by itself, but putting x++ inside other expressions or using x multiple times in one statement can produce surprising results because evaluation and the side effect happen in sequence. Example: y = arr[x++] + arr[x++]; first access uses x, then x becomes x+1, then second access uses the incremented x—not two reads of the same index. The AP CED explicitly says using ++ inside other expressions (like arr[x++]) is outside the exam scope because of these confusing side effects. To avoid bugs and be AP-safe, split it into clear steps: int idx = x; x = x + 1; val = arr[idx]; or val = arr[x]; x++; That follows EK 1.6 (post-increment behavior, side effects) and keeps your code readable. For a quick review, see the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1). For more practice, try the AP problems (https://library.fiveable.me/practice/ap-computer-science-a).

When should I use compound assignment operators instead of regular assignment?

Use compound assignment whenever you want to perform an arithmetic operation on a variable and then store the result back into that same variable—it's just a shorter, clearer way to write it. For example x += 5 is the same as x = x + 5; use +=, -=, *=, /=, %= to make code more concise and reduce duplication (EK 1.6.A.1). Compound ops are especially nice in loops and accumulators (for counters, sums, products). Be careful about semantics AP tests care about: /= and % follow Java’s rules (integer division, modulo), and compound assignment can involve implicit narrowing conversions for smaller types. Also know post-increment/post-decrement (x++, x--) changes the stored value but using them inside other expressions is out of scope for the exam (CED exclusion). Practice spotting final stored values—that’s LO 1.6.A. For a short review and examples see the AP study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL). For more practice (1000+ problems), check (https://library.fiveable.me/practice/ap-computer-science-a).

What does `score -= 10` actually do to the variable `score`?

score -= 10; subtracts 10 from score and stores the result back into score. It’s exactly the same as writing score = score - 10;—the compound assignment performs the indicated arithmetic between the left-side variable and the right-side value, then assigns that new value to the variable (EK 1.6.A.1). So if score was 42 before, after score -= 10 it becomes 32. This works for numeric primitives (int, double, etc.), and if the types differ Java may perform an implicit narrowing or widening conversion as required by the compound assignment rules. Note this is different from the post-decrement operator (score--), which subtracts 1. For more practice and quick review of compound assignment operators on the AP CSA CED, see the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL). If you want extra practice, Fiveable also has unit reviews and hundreds of practice problems (https://library.fiveable.me/ap-computer-science-a/unit-1 and https://library.fiveable.me/practice/ap-computer-science-a).

I don't understand how `remainder %= 3` works - can someone break it down?

Think of remainder %= 3 as shorthand for remainder = remainder % 3. The % operator (modulo) gives the remainder after division. So if remainder is 10, remainder %= 3 computes 10 % 3 (which is 1) and assigns 1 back into remainder. It’s a compound assignment (EK 1.6.A.1): do the operation between left and right, then store the result on the left. Important AP details: % works on integers the way you expect (10 % 3 → 1; 7 % 5 → 2). For negatives, Java keeps the sign of the left operand (e.g., -7 % 3 → -1), so be careful on exam questions. This is a simple assignment statement with a side effect—the variable on the left is updated. Practice spotting values after compound assignments for LO 1.6.A on the AP exam (see the Topic 1.6 study guide on Fiveable (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL)). For more practice problems, check Fiveable’s AP CSA practice set (https://library.fiveable.me/practice/ap-computer-science-a).

Are there any rules about using `++` and `--` operators that I need to know for the AP exam?

Short answer: yes—but the AP course keeps it simple. The CED only expects you to know the post-increment (x++) and post-decrement (x--) used as standalone assignment statements that add or subtract 1 from a numeric variable and store the new value. Important rules to remember for the exam: - Only post-form is in scope: x++ and x--. Prefix (++x/--x) is outside AP scope. - Don’t use ++ or -- inside other expressions (e.g., arr[x++], y = x++ + 2)—that usage is excluded from the course and exam. - They operate on numeric primitive variables and have the side effect of changing the variable’s stored value by ±1. - If you need combined arithmetic, use compound assignment operators (+=, -=, etc.) or explicit assignment so behavior is clear. For more review see the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1). For extra practice, Fiveable has 1000+ AP CSA problems (https://library.fiveable.me/practice/ap-computer-science-a).

How do compound assignment operators work with different data types like `int` and `double`?

Compound assignments do the operation then store the result in the left-side variable, but Java handles types specially: the result is implicitly narrowed to the left operand’s type. That means: - If left is int and right is double: a += b computes (left + right) then narrows/casts to int. Example: int a = 5; a += 2.5; // 5 + 2.5 = 7.5 -> narrowed to 7 (no compile error) - If you write the long form a = a + 2.5, that’s a compile error because a + 2.5 is double and you’re assigning to int without a cast. - If left is double and right is int: double d = 5.0; d += 2; // 7.0—no narrowing needed. - Watch integer division and expressions: int i = 5; i += 2/3; // 2/3 is int 0, so i unchanged. If one operand is double, division is double before narrowing. Remember post-increment/decrement (x++/x--) adds/subtracts 1 and is in scope for this topic (prefix form ++x is outside AP CSA scope). For more examples and AP-style practice, see the Topic 1.6 study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1). Practice problems there help solidify these cases.

What's the order of operations when using compound assignment operators in expressions?

Short answer: evaluate the right-hand expression first, perform the indicated arithmetic using the current left-hand value, then (if needed) implicitly cast the result to the left-hand variable’s type and store it back. Details that matter for AP CSA (CED-aligned): - Operators: +=, -=, *=, /=, %= do "left = left right" but with two differences: the right-hand side is fully evaluated before the operation, and Java performs an implicit narrowing conversion back to the left variable’s type if needed (e.g., short s; s += 2; is allowed even though s+2 would be int). - Post-increment/decrement (x++, x--) change the stored value by ±1; they return the old value but then update the variable. Note the CED excludes prefix ++ and using ++/-- inside other expressions on the exam, so expect simple uses. Examples: int a=5; a += 3*2; // rhs 3*2 = 6, a becomes 11 short s = 1; s += 2; // s becomes 3 (implicit cast) For more practice and a concise topic guide, see the Fiveable study guide (https://library.fiveable.me/ap-computer-science-a/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL) and try problems at (https://library.fiveable.me/practice/ap-computer-science-a).

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →