Task 9 of 14 · 0 solved · 14 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

1.4 Arithmetic — compound assignment

Lesson

Compound assignment

x += 5 is shorthand for x = x + 5: take the current value, add 5, store it back. The same works for -=, *=, and /=.


Worked example

int x = 10;
x += 5;   // x is now 15
x -= 3;   // x is now 12
x *= 2;   // x is now 24

▶ Try it: run this, then change something and run it again.



What does this print?

int x = 10;
x += 5;
System.out.println(x);

x += 1 is exactly the same as which line?


Type exactly what this prints:

int x = 4;
x *= 3;
System.out.println(x);

What does this print?

int x = 20;
x /= 4;
System.out.println(x);

Match each compound assignment to the same thing written out in full.