Compound Assignment Operators
Compound Assignment Operators#
We’ve already discussed the =
operator which sets the value of a variable. There are a few more assignment operators which are mostly used for convenience. These are the compound operators : +=
, -=
, *=
, /=
.
These operators apply their respective operation between the variable being assigned to and the value on the right and assign that value to the variable. In other words:
var += 2
can be read as
var = var + 2
and
var /= 2
can be read as
var = var / 2
etc..