What will be the value of x in following code:
int x=15;
x %= x++ + +x - 3
Please explain why.
Why doesn't it give syntax error for +x
or the extra +
before it?
What will be the value of x in following code:
int x=15;
x %= x++ + +x - 3
Please explain why.
Why doesn't it give syntax error for +x
or the extra +
before it?
x %= (x++) + (+x) - 3;
The x++
is called postincrement. You can check this post.
The +x
is just a sign operator.
So your statement will evaluate to:
x %= (15) + (+16) - 3;
You can split your code into parts like this to see how does it works:
int x = 15;
int y = x++ + +x;
System.out.println(y); //result: 31 (x = 15, then incremation so it is 15 + 16)
System.out.println(x); //result: 16 because of previous incrementation
y = y - 3;
System.out.println(y); //result: 31 - 3 = 28
x %= y;
System.out.println(x); //result: 16 % 28 = 16