1. Reference Guide

Term Definition / Example College Board Requirement
Compound assignment Does the math with the variable’s current value, then stores the result back in it: score += 10; Unit 1, Topic 1.6, p. 38
+= -= *= /= %= Shorthand for x = x op y: health -= 15; is health = health - 15; Unit 1, Topic 1.6, p. 38
Post-increment x++; adds 1 to x Unit 1, Topic 1.6, p. 38
Post-decrement x--; subtracts 1 from x Unit 1, Topic 1.6, p. 38
Tracing Writing the variable’s value after every line to predict the final value —
  • /= on an int is integer division: x = 7; x /= 2; leaves 3
  • =+ is not an operator: x =+ 5; means x = +5; and throws away the old value

Compound Assignment Operators

Operator Full Form Use Case Example
+= x = x + y Add to a total, or add text to a String score += 10
-= x = x - y Apply a penalty or a cost health -= 15
*= x = x * y Scale or apply a multiplier price *= 1.15
/= x = x / y Split or scale down (int division for int) total /= count
%= x = x % y Wrap around a board or clock position %= 10

Increment and Decrement

Operator Same As On the AP Exam?
x++; x += 1; Yes, as its own statement
x--; x -= 1; Yes, as its own statement
++x, --x prefix forms No
y = x++; or x++ + x++ increments inside another expression No

Tracing a Sequence

Line score after the line
int score = 100; 100
score += 50; 150
score -= 25; 125
score *= 2; 250
score /= 4; 62 (int division)
score %= 10; 2

Predicting the Result

  • Each line uses the value left by the line before it → trace one line at a time, in order
  • The variable is an int → /= drops the decimal, and %= keeps only the remainder
  • The variable is a double → *= and /= keep the decimal
  • ++ or -- appears inside a bigger expression → rewrite it as its own statement; that form is not tested

College Board Requirements

AP CSA Unit 1, Topic 1.6 Compound Assignment Operators. Quoted from the course and exam description (College Board, 2025, p. 38):

  • “Compound assignment operators +=, -=, *=, /=, and %= 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.”
  • “The post-increment operator ++ and post-decrement operator – are used to add 1 or subtract 1 from the stored value of a numeric variable. The new value is assigned to the variable.”

The course leaves out the prefix forms (++x, --x) and the use of ++ and -- inside other expressions, so this lesson uses them only as statements.

The Java language specification gives the exact meaning: “A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once” (Gosling et al., 2023, section 15.26.2).

Reference: College Board, AP Computer Science A Course and Exam Description (2025), Unit 1, Topic 1.6, p. 38.


2. Real-Life Application

The Arcade Level Counter That Ran Out of Room

A famous 1980 arcade maze game keeps its level number in one byte, which can only count up to 255. Every time you clear a level, the game adds one to that counter, like level++. On level 256, the code that draws the fruit icons along the bottom of the screen goes wrong, and half of the maze fills with garbled symbols. Nobody can finish that level, and players call it the “kill screen.” Everyone guess: why did the game’s designers not run into it during testing?

Real Case What Happened Java Idea in This Lesson
Arcade level counter (1980) Adding 1 to a full one-byte counter wrapped it around, and the fruit-drawing code broke level++ repeated many times; every counter has a limit
Bank balance A deposit adds to the balance and a fee takes from it balance += deposit; and balance -= fee;
Sale prices A 10% discount scales the price price *= 0.9;
Board games Moving past the last space wraps to the start position += roll; position %= 10;

The takeaway: a compound operator changes the variable it is applied to. To predict a program, trace the value after every update, in order.


3. Tech Talk

Learning Objective: Use compound assignment operators and ++/-- to update a variable, and trace the variable’s value after each line.

Success Criteria: You can rewrite long-form updates as compound operators, predict a variable’s value after a sequence of updates, and spot when /= or %= on an int drops the decimal.

A compound operator writes the variable name once instead of twice:


score = score + 10;   // long form
score += 10;          // compound form: the same thing
count++;              // count += 1

The type of the variable still matters:


int x = 7;
x /= 2;               // 3, because x is an int
double y = 7;
y /= 2;               // 3.5

On the AP exam, ++ and -- appear only as their own statements, like count++;.


Code Examples

A. One operator at a time. The CompoundTrace class prints the value after every update

Trace it before you run it. Write the value of num after each line, then check:


num += 5;    // 15
num /= 4;    // ?

Terminology:

Compound assignment reads the current value, does one operation with the value on the right, and stores the result back in the same variable.


Code Runner Challenge

Trace num on paper first, then run it and compare

View IPYNB Source
// CODE_RUNNER: Trace num on paper first, then run it and compare
public class CompoundTrace {
    public static void main(String[] args) {
        int num = 10;

        num += 5;
        System.out.println("After += 5: " + num);
        num -= 3;
        System.out.println("After -= 3: " + num);
        num *= 2;
        System.out.println("After *= 2: " + num);
        num /= 4;
        System.out.println("After /= 4: " + num);   // int division
        num %= 5;
        System.out.println("After %= 5: " + num);
        num++;
        System.out.println("After ++:   " + num);
        num--;
        System.out.println("After --:   " + num);
    }
}

CompoundTrace.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

B. Game state. The GameState class models a game turn with several variables

Real programs update several variables per event. Each update is its own statement, so it is easy to trace:


health -= 15;        // the enemy hits
enemiesDefeated++;   // one more defeated
damage *= 1.5;       // power-up: damage is a double

Terminology:

State is the set of values a program is tracking right now. Each compound operator changes exactly one piece of the state.


Code Runner Challenge

Predict the final state, run it, then add one more event

View IPYNB Source
// CODE_RUNNER: Predict the final state, run it, then add one more event
public class GameState {
    public static void main(String[] args) {
        int health = 100;
        int xp = 0;
        int level = 1;
        int enemiesDefeated = 0;
        double damage = 10.0;

        // Event 1: the enemy attacks
        health -= 15;

        // Event 2: you win the fight
        xp += 120;
        enemiesDefeated++;

        // Event 3: level up after 100 XP
        level++;
        xp -= 100;
        damage *= 1.5;

        // Event 4: a potion heals 30, capped at 100 by the next line
        health += 30;
        health = Math.min(health, 100);

        System.out.println("Health: " + health);
        System.out.println("XP: " + xp);
        System.out.println("Level: " + level);
        System.out.println("Enemies: " + enemiesDefeated);
        System.out.println("Damage: " + damage);
    }
}

GameState.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Hacks & Practice Tasks


Lesson: CSA 1.6 Compound Assignment Operators
MCQ 1.6: <paste the copied result line, such as 3/4 | answers: B,C,A,D>
Popcorn: all long-form updates rewritten as compound operators, output unchanged (yes/no); predicted health = <p>; actual = <a>
Homework Hack 1: traced value of x = <your prediction>, check prints true (yes/no)
Homework Hack 2: all three step tracker checks print true (yes/no)
Homework Hack 3: all three board game checks print true (yes/no); why %= = <one line>

Submission Safety Rules (Read First)

  • One class per cell, ending with ClassName.main(null);.
  • Label the homework hacks 1 through 3.
  • Write your Hack 1 trace before you run it.
  • Use ++ and -- only as their own statements, never ++x or inside another expression.
  • Run each Java cell and leave the output showing.
  • Include your MCQ score.
  • Use ## headings or smaller.

Popcorn Hack (In-Class)

3-minute challenge: rewrite every long-form update as a compound operator or ++.

  1. Rewrite the six update lines so no line looks like x = x + ...
  2. Keep every value the same: the output must not change
  3. Predict the final health in the Popcorn checker under the runner before you run
  4. Run the cell. When every check passes, copy the checker’s line into your submission notes

Code Runner Challenge

Rewrite the six updates with compound operators, predict health, then run it

View IPYNB Source
// CODE_RUNNER: Rewrite the six updates with compound operators, predict health, then run it
// Practice #1 - Try it yourself first!
public class GameScore {
    public static void main(String[] args) {
        int playerScore = 1000;
        int playerHealth = 100;
        int enemiesDefeated = 0;

        playerScore = playerScore + 250;        // defeat an enemy worth 250
        playerHealth = playerHealth - 15;       // take 15 damage
        enemiesDefeated = enemiesDefeated + 1;  // one more enemy defeated
        playerScore = playerScore * 2;          // boss bonus doubles the score
        playerHealth = playerHealth * 4;        // potion: keep 4/5 of health...
        playerHealth = playerHealth / 5;        // ...by multiplying by 4, then dividing by 5

        System.out.println("Score: " + playerScore);
        System.out.println("Health: " + playerHealth);
        System.out.println("Enemies Defeated: " + enemiesDefeated);
    }
}

GameScore.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Before you press Run, predict this output.

    Pass every check to finish the Popcorn Hack.

    4. MCQ Check

    4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your submission notes.

    5. Homework Hack

    Task: Do all three hacks. Each starter compiles and runs, so you can run the initial version and compare its incorrect output with the expected output. Change only what the TODO asks for, and keep going until every check prints true.

    Solution Skeleton:

    Hack 1: Trace It (5 minutes)

    Tracing. Work out the value of x after all six lines by hand, one line at a time. Put your answer in predictedX before you run. Do not change the six lines.

    Code Runner Challenge

    Trace x by hand, set predictedX, then run until the check prints true.

    View IPYNB Source
    // CODE_RUNNER: Trace x by hand, set predictedX, then run until the check prints true.
    public class TraceHack {
        public static void main(String[] args) {
            int x = 7;
            x += 5;
            x *= 3;
            x -= 6;
            x /= 4;
            x %= 4;
            x++;
    
            int predictedX = 0;  // TODO: your traced value, written BEFORE you run
    
            System.out.println("x = " + x);
            System.out.println(predictedX == x);
        }
    }
    TraceHack.main(null);
    
    Lines: 1 Characters: 0
    Output
    Click "Run" in code control panel to see output ...

    Hack 2: Step Tracker (5 minutes)

    Choosing the operator. Fill in each TODO with one compound operator or ++ statement. Day 1 and Day 3 are workout days. On the weekend challenge, the total steps double. Then work out the average steps per day using /= on an int.

    Code Runner Challenge

    Fill in each TODO with one compound operator or ++ so all three checks print true.

    View IPYNB Source
    // CODE_RUNNER: Fill in each TODO with one compound operator or ++ so all three checks print true.
    public class StepTracker {
        public static void main(String[] args) {
            int steps = 0;
            int workouts = 0;
            int days = 0;
    
            // Day 1: 8000 steps and a workout
            // TODO: add 8000 to steps, add 1 to workouts, add 1 to days
    
            // Day 2: 6500 steps, no workout
            // TODO: add 6500 to steps, add 1 to days
    
            // Day 3: 9000 steps and a workout
            // TODO: add 9000 to steps, add 1 to workouts, add 1 to days
    
            // Weekend challenge: double the total steps
            // TODO: double steps
    
            int average = steps;
            // TODO: divide average by days
    
            System.out.println("Steps: " + steps + ", workouts: " + workouts + ", average: " + average);
            System.out.println(steps == 47000);
            System.out.println(workouts == 2);
            System.out.println(average == 15666);
        }
    }
    StepTracker.main(null);
    
    Lines: 1 Characters: 0
    Output
    Click "Run" in code control panel to see output ...

    Hack 3: Board Game Turn (10 minutes)

    Combined. A board has 10 spaces, numbered 0 to 9. After each roll, move forward and wrap around past space 9 with %=. Then update the score and lives as the comments say. Write each update as a compound operator or --.

    Code Runner Challenge

    Write each update as a compound operator or -- so all three checks print true.

    View IPYNB Source
    // CODE_RUNNER: Write each update as a compound operator or -- so all three checks print true.
    public class BoardGame {
        public static void main(String[] args) {
            int position = 0;
            int score = 0;
            int lives = 3;
    
            // Roll 1: move 4, land on a coin worth 100
            // TODO: move forward 4, wrap with %= 10, add 100 to score
    
            // Roll 2: move 7, land on a trap
            // TODO: move forward 7, wrap with %= 10, lose 1 life with --, subtract 30 from score
    
            // Roll 3: move 9, land on a triple bonus
            // TODO: move forward 9, wrap with %= 10, multiply score by 3
    
            System.out.println("Position: " + position + ", score: " + score + ", lives: " + lives);
            System.out.println(position == 0);
            System.out.println(score == 210);
            System.out.println(lives == 2);
        }
    }
    BoardGame.main(null);
    
    Lines: 1 Characters: 0
    Output
    Click "Run" in code control panel to see output ...

    Grading Plan (1 Point Total)

    Part Points What earns the points
    Popcorn 0.2 Every long-form update rewritten as +=, -=, *=, /=, or ++, with the same output. The Popcorn checker line is in the notes.
    MCQ 0.2 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered.
    Homework Hack 1 0.15 predictedX written before running, and the check prints true.
    Homework Hack 2 0.2 One compound operator or ++ per update, /= for the average, and all three checks print true.
    Homework Hack 3 0.25 Movement wraps with %=, score and lives use compound operators and --, and all three checks print true.
    Total 1.0  

    Quick Validation Checklist

    • Each cell ends with ClassName.main(null); and shows output.
    • Popcorn checker line in the notes.
    • MCQ score in the notes.
    • No x = x + ... long forms left in the hacks.
    • No ++x, --x, or ++ inside another expression.
    • Every homework check prints true.

    6. LxD Cycle Process

    Empathize: Students write score = score + 10 when score += 10 says the same thing. More importantly, they lose track of a variable after several updates, forget that /= on an int drops the decimal, and mix up += with =+.

    Define:

    • POV: CSA students need to trace a variable through a sequence of compound updates, because the AP exam tests the final value after several lines, not the syntax alone.
    • Learning Goal: Students will apply every compound assignment operator, use ++ and -- as statements, and predict a variable’s value after a sequence of updates.

    Ideate:

    • HMW Question: How might we get students to trace one line at a time instead of guessing the final value?
    • HMW Question: How might we keep the practice about operators instead of about a complicated scenario?
    • Activity: Rewrite long-form game code with compound operators, trace a six-line sequence, then fill in the updates for a step tracker and a board game.

    Prototype:

    • A reference guide with a trace table, runnable trace and game-state examples, a rewrite Popcorn Hack with a checker, an MCQ knowledge check, three scaffolded homework hacks with self-checking output, and a grading rubric.
    • A trial run with open-ended scenarios showed students adding features instead of practicing operators, so each hack now gives the events in comments and asks for one operator per update.
    • Excellence means tracing each update correctly, including integer division with /= and wrap-around with %=.

    Test:

    • Ask peers in peer review to complete the practice without additional explanation.
    • Observe whether peers trace each line before running, and whether they predict the effect of /= on an int.
    • Compare your lesson with another that is posted.
    • Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
    • On submission, collect evidence from runner output, MCQ results, AI grading and student explanations.
    • After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.

    7. References/Citations

    College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025]. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf

    Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java® language specification (Java SE 21 ed., section 15.26.2, Compound assignment operators). Oracle. https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html

    past homework

    Submit Assignment

    This submits the code from this lesson's code runner(s) below as a Gist for automatic review. Save each runner with its 💾 button before submitting.

      Need to update a submission later? Open the submissions dashboard.