Pages

Friday, May 19, 2017

Compound Assignment



Compound Assignment

The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand.

The following table lists the compound assignment operators, example and its equivalent expressions:


Operator
Example
Equivalent expression

+=
number += 5;
number = number + 5;
-=
number -= 5;
number = number – 5;
*=
number *= 5;
number = number * 5;
/=
number /= 5;
number = number / 5;
&=
number &= 2;
number = number & 2;
%=
number %= 2;
number = number % 2;
^=
number ^= 2;
number = number ^ 2;
Compound assignment statement helps to shortens the expression and program length.

No comments:

Post a Comment