Back to Primitive Types

Lesson PreviewLesson 4 of 7 in this unit

1.4 Compound Assignment Operators

Part of Primitive Types in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

Compound Assignment Operators

Compound Assignment Operators are just ways to quickly change the value of a variable. Here, we demonstrate the += operator, which helps us quickly add something to the variable on the left side.

java
int dayOfChristmas = 4; //initialize dayOfChristmas to 4

System.out.print("On the ");
System.out.print(dayOfChristmas);
System.out.println("th day of Christmas, my true love sent to me-");

//ONE WEEK LATER
System.out.println("One week later...");

dayOfChristmas += 7;

System.out.print("On the ");
System.out.print(dayOfChristmas);
System.out.println("th day of Christmas, my true love sent to me-");

Output

On the 4th day of Christmas, my true love sent to me-
One week later...
On the 11th day of Christmas, my true love sent to me-

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.