int x = 99; int y = 10; y = --x ; System.out.println("x: " + x + " y: " + y );
x: 98 y: 98
+, -, *, /, (and others) can be
can be used with
= to make combined operators.
the following adds 5 to
sum += 5; // add 5 to sumThis statement has the same effect as:
sum = sum + 5; // add 5 to sum
Here is a list of some combined operators.
These operators work in three steps.
First, the complete expression on the right of the
= is evaluated.
Second, the operation that is combined with the
= is performed.
Finally the result is assigned to the variable.
|addition with assignment|
|subtraction with assignment|
|multiplication with assignment|
|division with assignment|
Here is a program fragment:
double w = 12.5 ; double x = 3.0; w *= x - 1 ; x -= 1 + 1; System.out.println( " w is " + w + " x is " + x );
What does this program fragment write out?