posted 3 years 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 post-unary operators have higher precedence over pre-unary 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 post-unary operators have higher precedence over pre-unary 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 3 years 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 3 years 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 post-unary or pre-unary 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 post-unary or pre-unary 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 OCPJP OCMJEA SOACP PMP

posted 3 years ago

That's absolutely

Those values are incorrect. Should be:

=> 4 * 5 / 4 + 2

=> 7

(the value of

And this is completely wrong as the post-unary operator is not executed first at all (as mentioned before).

With any other expression using post- and pre-unary 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

2/ Post-unary operators evaluated first (the

Hope it helps!

Kind regards,

Roel

K. Tsang wrote:Both explanations are correct. Either approach will get you the same result.

That's absolutely

__true! Expressions are evaluated from left to right, so post-unary operator is not executed first!__

**NOT**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 post-unary operator is not executed first at all (as mentioned before).

With any other expression using post- and pre-unary 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/ Post-unary operators evaluated first (the

__wrong__one):

`int j1 = 4 * 5 * 4; // i1 = 4, j1 = 80`

Hope it helps!

Kind regards,

Roel

posted 3 years 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 pre-increment, you

int y = 4 * 5 / ...

3. As you know, your "x" is 4 now. Next step you have post-decrement (x--). So you

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,

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 pre-increment, you

__increment "x" first__(3+1),__only then you place__it in expressionint y = 4 * 5 / ...

3. As you know, your "x" is 4 now. Next step you have post-decrement (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.

posted 3 years ago

Something worth noting is this kind of thing:

It will print 0.

It will print 0.

Consider Paul's rocket mass heater. |