Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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
 
Does this tiny ad smell okay to you?
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!