Kiruthika Dhanajeyan

Greenhorn

Posts: 4

posted 1 year ago

Hi,

post increment(++) or pre increment operators have highest precedence..

for eg:

int x = 4;

int y = x * 2 + ++x;

now according to the precedence x should increment to 5 first and then x is reassigned so the answer would be y = 5 * 2 + 5(y=15)..

But the right solution is y = 4* 2 + 5(y=13)..

Why and How??

Thanks in advance...

post increment(++) or pre increment operators have highest precedence..

for eg:

int x = 4;

int y = x * 2 + ++x;

now according to the precedence x should increment to 5 first and then x is reassigned so the answer would be y = 5 * 2 + 5(y=15)..

But the right solution is y = 4* 2 + 5(y=13)..

Why and How??

Thanks in advance...

posted 1 year ago

You are confusing precedence with evaluation order. Just because you (and actually, the majority of people) instinctively evaluate based on precedence, that doesn't mean the computer does that. The JLS specifies how expressions are evaluated, and for the most part, it is done left to right.

Henry

- 1

You are confusing precedence with evaluation order. Just because you (and actually, the majority of people) instinctively evaluate based on precedence, that doesn't mean the computer does that. The JLS specifies how expressions are evaluated, and for the most part, it is done left to right.

Henry

posted 1 year ago

Of course you are correct, Henry, but let's go further. In terms of evaluation order and precedence, how would you explain that this code outputs "7 7," rather than "9 7?"

Henry Wong wrote:The JLS specifies how expressions are evaluated, and for the most part, it is done left to right.Henry

Of course you are correct, Henry, but let's go further. In terms of evaluation order and precedence, how would you explain that this code outputs "7 7," rather than "9 7?"

"Il y a peu de choses qui me soient impossibles..."

Campbell Ritchie

Marshal

Posts: 55793

164

posted 1 year ago

- 1

The code is evaluated from left to right. You take 1 and add something to it. Then you find that the operator to the right of the two has a higher precedence, so you add the result of that expression evaluated from left to right to 1 and Bob's your uncle: 7.

There is no inconsistency between precedences and left‑to‑right.

There is no inconsistency between precedences and left‑to‑right.

Well, actually something like () behaves as if it had a higher precedence than the increment operators. The postfix operators have a higher precedence than the prefix operators, and prefix operators higher than multiplicative (* / %).Kiruthika Dhanajeyan wrote:post increment(++) or pre increment operators have highest precedence..

posted 1 year ago

evaluates from left to right

x *

4 *

Now checks is there any higher precedence operator than * then answer is yes i.e ++ so we get

4 * 2 + ( ++4 ) // ++ is evaluated first but x * 2 where value of x is already stored i.e 4

4 * 2 + ( 5 ) then

4 * again checks is there any higher precedence operator than * then answer is no then

( 4 * 2 ) + 5

8 + 5

8 + again checks is there any higher precedence operator than + then answer is no operator available then

8 + 5 = 13 is the answer.

4 *

4 * 2 + ( ++4 ) // ++ is evaluated first but x * 2 where value of x is already stored i.e 4

4 * 2 + ( 5 ) then

( 4 * 2 ) + 5

8 + 5

8 + 5 = 13 is the answer.

Being Java programmer.

posted 1 year ago

Same with

Evaluates from left

1 + checks any higher precedence operator than + so answer is yes i.e. * but we have in memory 1 + already so

1 + kept as it is and * is evaluated so

1 + ( 2 * 3 ) we get 1 + 6 then again checks any higher precedence operator than + ? then answer is no, so

(1 + 6) executed y = 7

2 * any higher precedence operator to right side than * ? answer is no then

( 2 * 3) + 1 so we get 6 + 1 again check higher than +? answer is no then

( 6 + 1 ) = 7 is the answer.

Evaluates from left

1 + kept as it is and * is evaluated so

(1 + 6) executed y = 7

**Same with 2 * 3 + 1**

( 2 * 3) + 1 so we get 6 + 1 again check higher than +? answer is no then

( 6 + 1 ) = 7 is the answer.

Being Java programmer.

Kiruthika Dhanajeyan

Greenhorn

Posts: 4