Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operator presedence

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I found it in jqplus.
 
rohan yadav
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expression is evaluated to x = (x) +(AN_INT+(++x))
that's why output is 16
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the lass ++x wont effect the actual value of x??
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think its because of ( x += AN_INT)+((++x))) becasue +comes first in the hierarchy
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minhaj kaimkhani wrote:

Hope its clear now..
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then why is x*=7+5; not x=x*7+5; ?
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe its becasue its x*(7+5) becasue of that rule agian
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandana Garlapati wrote:
Minhaj kaimkhani wrote:

Hope its clear now..
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment operators have right to left associativity.i.e; the expression is evaluated from right to left.
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the output should be 17 right Chandana?
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the answer is 16.

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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neha can you explain in detail?
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ones at the right should be performed before the ones at the left.
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic