Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# operator presedence

Minhaj Mehmood
Ranch Hand
Posts: 400

I found it in jqplus.

Ranch Hand
Posts: 156
Expression is evaluated to x = (x) +(AN_INT+(++x))
that's why output is 16

Minhaj Mehmood
Ranch Hand
Posts: 400
the lass ++x wont effect the actual value of x??

Ankur kothari
Ranch Hand
Posts: 531
i think its because of ( x += AN_INT)+((++x))) becasue +comes first in the hierarchy

Ankur kothari
Ranch Hand
Posts: 531

here x is 84....according to K&B right of += is calculated first...

so minhaj i think your answer should be x+=3+7; which is x=7+10...17

then why 16? hmmmm

Chandana Garlapati
Ranch Hand
Posts: 97
Minhaj kaimkhani wrote:

Hope its clear now..

Ankur kothari
Ranch Hand
Posts: 531
then why is x*=7+5; not x=x*7+5; ?

Ankur kothari
Ranch Hand
Posts: 531
maybe its becasue its x*(7+5) becasue of that rule agian

Minhaj Mehmood
Ranch Hand
Posts: 400
Chandana Garlapati wrote:
Minhaj kaimkhani wrote:

Hope its clear now..

Ankur kothari
Ranch Hand
Posts: 531
maybe its becasue the operands are evaluated from left to right....

i think this is x+= (AN_INT) + (++x).....first += is opened up soit is

its x=(x) + (AN_INT) + (++x)

this is simply so confusing

Chandana Garlapati
Ranch Hand
Posts: 97
Assignment operators have right to left associativity.i.e; the expression is evaluated from right to left.

Ankur kothari
Ranch Hand
Posts: 531
so the output should be 17 right Chandana?

Neha Daga
Ranch Hand
Posts: 504

when post increment is applied second time the value is stored temporarily and after all calculations final value is assigned to x.

Ankur kothari
Ranch Hand
Posts: 531
neha can you explain in detail?

Chandana Garlapati
Ranch Hand
Posts: 97
ones at the right should be performed before the ones at the left.

Ankur kothari
Ranch Hand
Posts: 531
ok so correct me if am wrong

here x is 6;
x+=3 + ++x;

so Chandana according to you it would be x+=3+ 7; which evaluates to x+=10;

which is x=x+10;

which is x=10+7; right? or 10+6?

Chandana Garlapati
Ranch Hand
Posts: 97
As Neha said, value is stored temporarily and after performing the right expression then x value is added...

lets try this

Now the output is 17..

Ankur kothari
Ranch Hand
Posts: 531
leave it...why dont either of you simply show me the calculation step by step...the answer is 16....maybe Ankit or Henry could explain properly

Neha Daga
Ranch Hand
Posts: 504
ok..expression is x+= AN_INT + ++x which is x = x + AN_INT + ++x
now according to the rule right side will be calculated first but from left to right
so it becomes: 6+ 3 +7=16.

now, I hope its clear.

Raju Champaklal
Ranch Hand
Posts: 521
i just read the jls and there it is mentioned that the program rememebrs what you had on the left hand side...so its like x+=3+7 whcih gives x+=10 which is x=x+10 equals x=6+10 which is 16......Bingo

understood Ankur???

Raju Champaklal
Ranch Hand
Posts: 521