CODE
class CompoundAssignments
{
public static void main(String arg[])
{
int x = 5;
int y = 2;
int z = 3;
x += 6; // LINE A
y *= 8; // LINE B
z += y * x; // LINE C
z %= 7; // LINE D
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}
}
x = 11
y = 16
z = 4
Here, we have declared three variables x
, y
and z
and initialized them.
In LINE A
, 6
is added to previous x
value, which is 5
and assigned to x
again. So x
becomes 11 (= 5 + 6)
.
In LINE B
, the value of y
is changed to 16 (= 8 * 2)
from 2
.
In LINE C
, the y * x (= 16 * 11 = 176)
is added to z
and assigned to z
again. So z
changes from 3
to 179 (= 176 + 3)
.
In LINE D
, the value of z % 7
is assigned to z
, so z
becomes 4 (= 179 % 7)
.
A, B, C
and D
to B, A, D
and C
and see what will be the output. Try some other order of lines and check if the output is per your understanding.x
is 11
, y
is 16
and z
is 25
at the end of the program.-=
and /=
.