Assignment arithmetic
Each of the above operators can be combined with the assignment operator, =, as a shortcut for performing operations. Such assignments use the value to the right of the assignment operator to perform an operation with the value to the left. The result of the operation is then assigned to the value on the left.
= |
assignment |
assigns a value to a variable |
+= |
assign addition |
adds a value to a variable |
-= |
assign subtraction |
subtracts a value from a variable |
*= |
assign multiplication |
multiplies a variable by a value |
/= |
assign division |
divides a variable by a value |
%= |
assign remainder |
returns a remainder after division |
The following lines are examples using assignment arithmetic.
var i; |
||
i = 2; |
i is now 2 |
|
i += 3; |
i is now 5, (2+3) |
same as i = i + 3 |
i -= 3; |
i is now 2, (5-3) |
same as i = i - 3 |
i *= 5; |
i is now 10, (2*5) |
same as i = i * 5 |
i /= 3; |
i is now 3, (10/3) |
same as i = i / 3 |
i = 10; |
i is now 10 |
|
i %= 3; |
i is now 1, (10%3) |
same as i = i % 3 |
Auto-increment (++) and auto-decrement (--)