Skip to the content.

Compound Assignment Operators

Student Teaching Lesson

Popcorn Hack 1

add compound assignment operators (+=, -=, *=, /=)

int playerScore = 1000;
int playerHealth = 100;
int enemiesDefeated = 0;

// Player defeats an enemy worth 250 points
playerScore += 250; 

// Player takes 15 damage
playerHealth -= 15;

// Enemy count goes up
enemiesDefeated += 1;

// Boss battle: double the current score!
playerScore *= 2;

// Healing potion restores health to 80% of current
playerHealth *= 4;
playerHealth /= 5;  // 4/5 = 0.8, but we keep integer math
68

Popcorn Hack 2

public class ScoreUpdater {
    public static void main(String[] args) {
        int score = 100;
        System.out.println("Starting score: " + score);

        // Deduct points for a wrong answer
        score -= 30;
        System.out.println("After wrong answer (-=): " + score);

        // Double the score with a power-up
        score *= 2;
        System.out.println("After power-up (*=): " + score);

        // Find remainder after dividing by 7
        score %= 7;
        System.out.println("After remainder (%=): " + score);
    }
}
ScoreUpdater.main(null);
Starting score: 100
After wrong answer (-=): 70
After power-up (*=): 140
After remainder (%=): 0

Assignment

public class InfluencerSim {
    public static void main(String[] args) {
        // Starting statistics for the influencer
        int followers = 500;           // Number of followers
        int posts = 10;                // Total posts made
        int engagement = 250;          // Total likes and comments
        int sponsorshipEarnings = 0;   // Total money earned from sponsors in dollars

        System.out.println("Stats");
        System.out.println("Followers: " + followers);
        System.out.println("Posts: " + posts);
        System.out.println("Engagement: " + engagement);
        System.out.println("Sponsorship Earnings: $" + sponsorshipEarnings);
        System.out.println();

        // Viral post → gain followers
        followers += 1000;  // += adds 1000 followers
        System.out.println("Viral post! Followers increased to: " + followers);

        // Controversial post → lose some followers
        followers -= 75;   // -= subtracts 75 followers
        System.out.println("Controversy! Followers dropped to: " + followers);

        // Trending hashtag → double engagement
        engagement *= 2;   // *= multiplies engagement by 2
        System.out.println("Trending hashtag! Engagement doubled to: " + engagement);

        // Calculate average engagement per post
        engagement /= posts;  // /= divides total engagement by number of posts
        System.out.println("Average engagement per post: " + engagement);

        // Secure a sponsorship deal → increase earnings
        sponsorshipEarnings += 500;  // += adds $500 sponsorship money
        System.out.println("Sponsorship deal secured! Total earnings: $" + sponsorshipEarnings);

        // Daily upload streak → increase post count
        posts++;  // ++ increments posts by 1
        System.out.println("Posted a new video! Total posts: " + posts);

        // Ranking among top 100 creators
        int rankingPosition = 342;
        rankingPosition %= 100;  // %= gets remainder to find position in top 100
        System.out.println("Your current top 100 ranking: " + rankingPosition);
    }
}
InfluencerSim.main(null);
Stats
Followers: 500
Posts: 10
Engagement: 250
Sponsorship Earnings: $0

Viral post! Followers increased to: 1500
Controversy! Followers dropped to: 1425
Trending hashtag! Engagement doubled to: 500
Average engagement per post: 50
Sponsorship deal secured! Total earnings: $500
Posted a new video! Total posts: 11
Your current top 100 ranking: 42