Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

evaluating logical short-circuit operators ???

 
David Swan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me how to evaluate this line in the following code: if (true && false | true)
should you start evaluating from left to right ?? and where does the compiler put parentheses into this statement. Heres the full code, "hi" is output to the screen.
class test {
public static void main(String[] args) {
if (true && false | true)
System.out.println("hi");
}
}
Thanks
Dave
 
David Swan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also here, which && operator works on which boolean values???
class test {
public static void main(String[] args) {
if (true && true && false)
System.out.println("hi");
}
}
Cheers
Dave
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The precedence of these operators is this:
&
^
|
&&
||
This means in your case that first | is evaluated on both values because it isn't a shortcut operator and then with the result of the previous && is evaluated. In your case you would get something like this (true && (false|true))
hope this helps?
Rikko
 
David Swan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers rikko
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic