Operators: +=, -=, *=, /=, %=, <<=, >>=, &=, |= ^=

Compound assignment operator.

Syntax

destination‑operand+=operand

destination‑operand-=operand

destination‑operand*=operand

destination‑operand/=operand

destination‑operand%=operand

destination‑operand<<=operand

destination‑operand>>=operand

destination‑operand&=operand

destination‑operand|=operand

destination‑operand^=operand

Compound assignment

The compound assignment operators combine the assignment = with another regular operation. When executed, the operator first performs the regular operation with the both operands found on its left and right side and then, in second step, assigns the result of the operation to the operand on its left side. One example is the addition assignment operator +=. It implies a regular addition + operator and the assignment operator. As such it can be considered as abbreviation for the more complex operation a = a + b:

var string str = "Hello"; var int32 num = 1369; str += " World"; // str = "Hello World" num += 1; // num = 1370

The following table provides an overview of the supported compound assignment operators and the resulting operations:

Operator

Equivalent to

a += b

a = a + b

a -= b

a = a - b

a *= b

a = a * b

a /= b

a = a / b

a %= b

a = a % b

a >>= b

a = a >> b

a <<= b

a = a << b

a &= b

a = a & b

a |= b

a = a | b

a ^= b

a = a ^ b

a &= b

a = a && b

a |= b

a = a || b

Compound assignment and logical && and || operators

The compound assignment operators involving the && (logical AND) or || (logical OR) operators are represented by the operators &= and |= (with single & or | letter in their name). There is no separate version of these operators like &&=. The following example demonstrates the usage of the compound assignment operator performing the logical AND operation:

var bool b1 = true; var bool b2 = true; var bool b3 = false; b1 &= b2; // b1 = true b1 &= b3; // b1 = false

Implicit type conversion

The Chora compound assignment operators subject the result of the operation to an implicit (automatic) type conversion if the data type of the result does not match the data type of the destination operand on the left of the compound assignment operator. In the following example, the addition operation results in a float value which then when assigning it to the variable a is converted implicitly in int32 data type:

var int32 a = 1369; var float b = 12.51; // Convert automatically the int32 value to the float data type a += b; // a = 1381

Chained compound assignment

A compound assignment operation can be embedded as operand within another more complex operation. In this way a single expression can perform multiple assignments to multiple variables or the assignments can be chained. Following example demonstrates the function. Here you can consider the single expression as being composed of two partial expressions d += a + b and e += d + c:

var int32 a = 10; var int32 b = 20; var int32 c = 30; var int32 d = 7; var int32 e = 89; e += ( d += a + b ) + c; // d = 37, e = 156

When chaining assignment operators you should note the data type resulting from the nested assignment. As explained in the section above the compound assignment operator performs an implicit type conversion to ensure that the assigned value does correctly conform the data type of the destination operand. If this assignment is part of another more complex expression, the value it represents as operand within this expression is the value after the implicit type conversion took place.

The following example performs a chained assignment first to an int8 integer variable b and then the result of this operation to an int32 variable c. During the assignment to the variable b the result of the associated addition is converted to fit in the 8-bit integer. Since it is too large (a+b = 150) it is 8-bit truncated and overflows to the value -106. This value -106 is consequently used in the second compound assignment to the variable c:

var int32 a = 50; var int8 b = 100; var int32 c = 200; c += b += a; // b = -106, c = 94