After evaluating the expression, the op operator is then applied to the result of the expression and the current value of the variable (on the RHS). The result of this operation is then assigned back to the variable (on the LHS).

Let’s take some examples:

The statement:

1

x+=5;

is equivalent to x = x + 5; or x = x + (5);.

Similarly, the statement:

1

x*=2;

is equivalent to x = x * 2; or x = x * (2);.

Since, expression on the right side of op operator is evaluated first, the statement:

1

x*=y+1;

is equivalent to x = x * (y + 1).

The precedence of compound assignment operators are same and they associate from right to left (see the precedence table).

The following table lists some Compound assignment operators:

Operator

Description

+=

x += 5 equivalent to x = x + 5

-=

y -= 5 equivalent to y = y - 5

/=

z /= 3 equivalent to z = z / 5

%=

m %= 10 equivalent to m = m % 10

The following program demonstrates Compound assignment operators in action: