• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Boolean Expressions

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
howdy all



I'm a little confused with the above. The output is "1".
The first statement, does it read "true or false", which results in a true, then "true and true" gives the first expression true which prints out "1".

So does it means it always works from left to right, given that there's no parenthesis?

thanks
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

It evaluates left to right if it doent have paranthesis.


output : -1.
Note that ~ operator has higher precedance.

Thanks
Praveen SP
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brandon Bay:

So does it means it always works from left to right, given that there's no parenthesis?



The operators always works according to the operator precedence not left to right always.
Note that, &(AND) has higher precedence than ^(XOR) which has higher precedence than |(OR)

You can test the statement using this code,

[ September 12, 2007: Message edited by: Al Mamun ]
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Al mamum

But in the example I gave,~ has higher precedance than increment operators,but still it executed last.WHY?

Thanks
Praveen SP
 
Abdullah Mamun
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Praveen Seluka:

hi Al mamum

But in the example I gave,~ has higher precedance than increment operators,but still it executed last.WHY?



I think the reason is what I found in the JLS,

The Java programming language guarantees that every operand of an operator ( except the conditional operators &&, ||, and ? : ) appears to be fully evaluated before any part of the operation itself is performed.

If the binary operator is an integer division / or integer remainder %, then its execution may raise an ArithmeticException, but this exception is thrown only after both operands of the binary operator have been evaluated and only if these evaluations completed normally.



Also note that, postfix operator(expr++) has higher precedence than (~expr)

For further detail please follow this link
[ September 12, 2007: Message edited by: Al Mamun ]
 
Die Fledermaus does not fear such a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic