x: 98 y: 98
The operators +, -, *, /,
(and others) can be
can be used with =
to make combined operators.
For example,
the following adds 5 to sum
:
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:
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; |
These operators work in three steps.
First, the complete expression on the right of the =
is evaluated.
Second, the operation that is part of the =
is performed.
Finally the result is assigned to the variable.