Win a copy of Spring Boot in Practice this week in the Spring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# operator presedence

Ranch Hand
Posts: 400   • • Number of slices to send:
Optional 'thank-you' note:
• • I found it in jqplus.

Ranch Hand
Posts: 156
• • Number of slices to send:
Optional 'thank-you' note:
• • Expression is evaluated to x = (x) +(AN_INT+(++x))
that's why output is 16

Minhaj Mehmood
Ranch Hand
Posts: 400   • • Number of slices to send:
Optional 'thank-you' note:
• • the lass ++x wont effect the actual value of x??

Ranch Hand
Posts: 531
• • Number of slices to send:
Optional 'thank-you' note:
• • i think its because of ( x += AN_INT)+((++x))) becasue +comes first in the hierarchy

Ankur kothari
Ranch Hand
Posts: 531
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Ranch Hand
Posts: 97
• • Number of slices to send:
Optional 'thank-you' note:
• • Minhaj kaimkhani wrote:

Hope its clear now..

Ankur kothari
Ranch Hand
Posts: 531
• • Number of slices to send:
Optional 'thank-you' note:
• • then why is x*=7+5; not x=x*7+5; ?

Ankur kothari
Ranch Hand
Posts: 531
• • Number of slices to send:
Optional 'thank-you' note:
• • maybe its becasue its x*(7+5) becasue of that rule agian

Minhaj Mehmood
Ranch Hand
Posts: 400   • • Number of slices to send:
Optional 'thank-you' note:
• • Chandana Garlapati wrote:

Minhaj kaimkhani wrote:

Hope its clear now..

Ankur kothari
Ranch Hand
Posts: 531
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • Assignment operators have right to left associativity.i.e; the expression is evaluated from right to left.

Ankur kothari
Ranch Hand
Posts: 531
• • Number of slices to send:
Optional 'thank-you' note:
• • so the output should be 17 right Chandana?

Ranch Hand
Posts: 504
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • neha can you explain in detail?

Chandana Garlapati
Ranch Hand
Posts: 97
• • Number of slices to send:
Optional 'thank-you' note:
• • ones at the right should be performed before the ones at the left.

Ankur kothari
Ranch Hand
Posts: 531
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

Ranch Hand
Posts: 521
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• •  Don't get me started about those stupid light bulbs.