The table below lists the compound assignment operators and shows an expression using each operator:
Operator | Example | Equivalent Expression |
---|---|---|
+= | index += 2 | index = index + 2 |
-= | *(pointer++) -= 1 | *pointer = *(pointer++) - 1 |
*= | bonus *= increase | bonus = bonus * increase |
/= | time /= hours | time = time / hours |
%= | allowance %= 1000 | allowance = allowance % 1000 |
<<= | result <<= num | result = result << num |
>>= | form >>= 1 | form = form >> 1 |
&= | mask &= 2 | mask = mask & 2 |
^= | test ^= pre_test | test = test ^ pre_test |
|= | flag |= ON | flag = flag | ON |
Although the equivalent expression column shows the left operands (from the example column) evaluated twice, the left operand is evaluated only once.