Assignment operators
The assignment operators are considered to be the binary operators. The left-hand operand of an assignment operation must be a variable, item of array, field of structure etc. These operators have right-to-left associativity.
= | Simple assignment. |
+= | Addition assignment. a += b => a = a + b |
-= | Subtraction assignment. a -= b => a = a - b |
*= | Multiplication assignment. a *= b => a = a * b |
/= | Division assignment. a /= b => a = a / b |
%= | Remainder assignment. a %= b => a = a % b |
&= | Bitwise-AND assignment. a &= b => a = a & b |
=) | |
^= | Bitwise-exclusive-OR assignment. a ^=b => a = a ^ b |
>>= | Right-shift assignment. a >>= b => a = a >> b |
<<= | Left-shift assignment. a <<= b => a = a << b |
As you have already noticed, except "simple assignment" you can perform the assignment with an operation, that is after a binary operation of the right-hand operand and the left-hand operand is performed, the result is assigned into the left operand.
a = 10
a += 10 + 23 // a = 43
a *= 2 // a = 86
if a = 2 // TRUE !!!
{...}
if a == 2 // TRUE if a equals 2
{...}
One and the same expression can contain several assignment operations, each of which returns the assigned value. In this case, the assignment operation is performed from right to left.
a = 10 + b = 20 + c = 3
// result: с=3, b=23, a=33
a = ( b += 10 )
You can define these operators for any types. See more details on the Redefining operator operations page.