int x = 99;
int y = 10;
y = --x ;
System.out.println("x: " + x + " y: " + y );
x: 98 y: 98
| Operator | Operation | Example | Effect |
|---|---|---|---|
= | assignment | sum = 5; | sum = 5; |
+= | addition with assignment | sum += 5; | sum = sum + 5; |
-= | subtraction with assignment | sum -= 5; | sum = sum - 5; |
*= | multiplication with assignment | sum *= 5; | sum = sum * 5; |
/= | division with assignment | sum /= 5; | sum = sum/5; |
The operators +, -, *, / make combined operators
with =.
For example,
the following adds 5 to sum:
sum += 5; // add 5 to sum
This statement has the same effect as:
sum = sum + 5; // add 5 to sum
Here is how += works.
+= is evaluated.+ adds that to the value in the variable.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?