Post and Pre unary operator precedence (Java OCA 8 Programmer I Study Guide)
Alan Couze
Greenhorn
Posts: 5
posted 1 year ago
In chapter 2 on page 57 there is an example on the pre/post increment and decrement operators:
It explains that first, the x is incremented and returned:
However, on page 51, Table 2.1 it says that postunary operators have higher precedence over preunary operators. So in this example why wouldn't the x be decremented to 2 first and return 3?
Then x would be incremented to 3
...and then x would be decremented back to 2:
This yields the same result that the book states, but just in a different manner. Which is correct?
It explains that first, the x is incremented and returned:
However, on page 51, Table 2.1 it says that postunary operators have higher precedence over preunary operators. So in this example why wouldn't the x be decremented to 2 first and return 3?
Then x would be incremented to 3
...and then x would be decremented back to 2:
This yields the same result that the book states, but just in a different manner. Which is correct?
posted 1 year ago
This topic has a great explanation about the same code snippet. If you still have doubts after reading this topic, let us know by simply hitting the "Post Reply" button
Hope it helps!
Kind regards,
Roel
Alan Couze wrote:This yields the same result that the book states, but just in a different manner. Which is correct?
This topic has a great explanation about the same code snippet. If you still have doubts after reading this topic, let us know by simply hitting the "Post Reply" button
Hope it helps!
Kind regards,
Roel
posted 1 year ago
When it comes to pre and post increment/decrement, the assign first or increment/decrement first needs to be clear.
Post increment / decrement (x++) is assign then increment/decrement
Pre increment / decrement (x) is increment/ decrement then assign
Both explanations are correct. Either approach will get you the same result.
Using the rule above, working from left to right applying the postunary or preunary all at once is probably the straightforward way, despite the post unary has higher precedence.
y = ++x * 5 / x + x
using left to right post and pre unary apply all at once
=> 4 * 5 / 3 + 1
=> 7
or using post unary first
=> 3 * 5 / 3 + 2
=> 7
Post increment / decrement (x++) is assign then increment/decrement
Pre increment / decrement (x) is increment/ decrement then assign
Both explanations are correct. Either approach will get you the same result.
Using the rule above, working from left to right applying the postunary or preunary all at once is probably the straightforward way, despite the post unary has higher precedence.
y = ++x * 5 / x + x
using left to right post and pre unary apply all at once
=> 4 * 5 / 3 + 1
=> 7
or using post unary first
=> 3 * 5 / 3 + 2
=> 7
K. Tsang OCPJP7 OCMJEA6
posted 1 year ago
That's absolutely NOT true! Expressions are evaluated from left to right, so postunary operator is not executed first!
Those values are incorrect. Should be:
=> 4 * 5 / 4 + 2
=> 7
(the value of x will never be 1)
And this is completely wrong as the postunary operator is not executed first at all (as mentioned before).
With any other expression using post and preunary operators, the result will not be the same. So you can't conclude that both explanations are correct just because for one expression the results are the same. Here is an example that disproves both explanations being correct!1/ Expression evaluated from left to right (the correct one): int j1 = 6 * 6 * 5; // i1 = 4, j1 = 180
2/ Postunary operators evaluated first (the wrong one): int j1 = 4 * 5 * 4; // i1 = 4, j1 = 80
Hope it helps!
Kind regards,
Roel
K. Tsang wrote:Both explanations are correct. Either approach will get you the same result.
That's absolutely NOT true! Expressions are evaluated from left to right, so postunary operator is not executed first!
K. Tsang wrote:using left to right post and pre unary apply all at once
=> 4 * 5 / 3 + 1
=> 7
Those values are incorrect. Should be:
=> 4 * 5 / 4 + 2
=> 7
(the value of x will never be 1)
K. Tsang wrote:or using post unary first
=> 3 * 5 / 3 + 2
=> 7
And this is completely wrong as the postunary operator is not executed first at all (as mentioned before).
With any other expression using post and preunary operators, the result will not be the same. So you can't conclude that both explanations are correct just because for one expression the results are the same. Here is an example that disproves both explanations being correct!1/ Expression evaluated from left to right (the correct one): int j1 = 6 * 6 * 5; // i1 = 4, j1 = 180
2/ Postunary operators evaluated first (the wrong one): int j1 = 4 * 5 * 4; // i1 = 4, j1 = 80
Hope it helps!
Kind regards,
Roel
posted 1 year ago
 1
Alan,
You always should try in practice after you read, so you could check if you're right. How to check?
Here is how I think in my head about them ("++x", "x", "x"), like about a standalone expressions.
At the beginning you have x = 3.
1. x = 3
2. Since it is preincrement, you increment "x" first (3+1), only then you place it in expression
int y = 4 * 5 / ...
3. As you know, your "x" is 4 now. Next step you have postdecrement (x). So you first place your current "x" value to expression, then decrement.
int y = 4 * 5 / 4 + ...
4. You know from previous step. That your "x" after was placed to expression, then had to be decremented (4  1 = 3). So the last one you have is "x". And current "x" is 3. So again, decrement first again, and only then place to expression.
int y = 4 * 5 / 4 + 2
5. And, since first multiplication/division, then addition, you should think about it as:
int y = ((4 * 5) / 4) + 2
So you have:
int y = 20 / 4 + 2;
int y = 5 + 2;
int y = 7;
Hope it helps a bit.
You always should try in practice after you read, so you could check if you're right. How to check?
Here is how I think in my head about them ("++x", "x", "x"), like about a standalone expressions.
At the beginning you have x = 3.
1. x = 3
2. Since it is preincrement, you increment "x" first (3+1), only then you place it in expression
int y = 4 * 5 / ...
3. As you know, your "x" is 4 now. Next step you have postdecrement (x). So you first place your current "x" value to expression, then decrement.
int y = 4 * 5 / 4 + ...
4. You know from previous step. That your "x" after was placed to expression, then had to be decremented (4  1 = 3). So the last one you have is "x". And current "x" is 3. So again, decrement first again, and only then place to expression.
int y = 4 * 5 / 4 + 2
5. And, since first multiplication/division, then addition, you should think about it as:
int y = ((4 * 5) / 4) + 2
So you have:
int y = 20 / 4 + 2;
int y = 5 + 2;
int y = 7;
Hope it helps a bit.
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/ThreadBoostfeature
