Could you explain step by step how java evaluates
1) the value of y ?
int x = 5; int y = x-- % x++;
2) the value of y in this case?
int x = 5; int y = x-- * 3 / --x;
Could you explain step by step how java evaluates
1) the value of y ?
int x = 5; int y = x-- % x++;
2) the value of y in this case?
int x = 5; int y = x-- * 3 / --x;
I'm not sure about java but in C it evolved into undefined behaviour.
Well, the operands are evaluated from left to right, and in each case the result of a postfix operation is the value of the variable before the increment/decrement whereas the result of a prefix operation is the value of the variable after the increment/decrement... so your cases look like this:
Case 1:
Case 2:
Personally I usually try to avoid using pre/post-increment expressions within bigger expressions, and I'd certainly avoid code like this. I find it's almost always clearer to put the side-effecting expressions in separate statements.