Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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!