A compound assignment operator is an operator that performs both an implicit calculation and an assignment.
Here’s a list of all the compound assignment operators for the arithmetic operators.
Operator | Description |
---|---|
+= | Addition and assignment |
-= | Subtraction and assignment |
*= | Multiplication and assignment |
/= | Division (for quotient) and assignment |
%= | Division (for remainder) and assignment |
For example, the statement a = a + 10
can be written as a += 10
.
Here’s another exampe. The statement b = b * 100
can be written as b *= 100
.
Compound assignment operators can be confusing when used in complex expressions. Therefore, we recommend you to always compound assignment operators by themselves.