Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Operator Precedence  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code prints true false false. This may be because it is evaluated from left to right and as "a" is true and short circuit operator is used rest of the expression is not evaluated.
My doubt here is that && has higher precedence than || then why isn't it evaluated first. Something like


Result here is 13 and not 18, that means * was done before + as * has higher than precedence +.
Why doesn't this apply to first case?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, || and && have the same priority so once a is true the rest is skipped.
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
No, || and && have the same priority so once a is true the rest is skipped.



Just saw this.

http://www.cs.indiana.edu/~kinzler/home/etc/opr-java

for && precedence is mentioned 10 and for || precedence is mentioned 11.
Not sure if this is a correct table.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even if, it doesn't matter.

Your logical evaluation will be left to right with the leftmost element being true which if there's an OR in there will ensure the entire thing is true.

Using assignments in a logical evaluation expression is of course also extremely bad coding practice...
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.
But why doesn't that happen here? Why is 4*2 evaluated first?

[ June 29, 2004: Message edited by: Anshul Kayastha ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BODMAS
thats because multiply has more precedence than add
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by subu ananthram:
BODMAS
thats because multiply has more precedence than add



I was asking this in context of the above question
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!