• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if condition problem ( ex from k&b )

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm having problem understanding the conditions explanation in k&b.

it says:




1. I don't know if its a typo, should it be || instead of |. Would it still return a boolean with |.?

2. Assuming it was a typo and it actually meant ||, k&b says:

if (x>3) is false, no point in looking at the rest of the expression. 'cos during runtime it is evaluated as




but my tests prove otherwise. my test proves, left to right operation :



which means, it solves the first operation from left to right and the result of it, it operates with the next one.

can someone shed some light on this?

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

Originally posted by M Rama:

if (x>3) is false, no point in looking at the rest of the expression. 'cos during runtime it is evaluated as




I think you'd be right if it was a "||" (boolean OR). But notice that it's a "|" (bitwise OR). As it turns out, bitwise OR has precendence over boolean AND.

So, if I'm understanding it correctly, the expression is evaluated like this:



So, doStuff() should always be evaluated. It's not susceptible to "short circuiting" by a false cropping up earlier in the expression.

Regards,

- Jeff
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Rama,
First of all, one must clearly remember the precedence and then the associativity
As of this case, the operators under discussion are & && | ||
The order of precedence is
Bitwise / logical AND&
Bitwise / logical OR|
Conditional AND &&
Conditional OR ||

Case (1)
System.out.println(false && true || true);//returns true
Operators involved are && ||
In this && has highest precedence. So the expression would be evaluated as
((false && true) || true)
(false || true)
(true)

Case (2)
System.out.println(false && true | true);//returns false
Operators involved && |
In this | has highest precedence. So the expression would be evaluated as
(false && ( true | true))
(false && true)
(false)

I hope this helps.
Please do mail back if what I have understood is not you have asked for.

Cheers !!!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi da Rama,

if(((x>3)&&(y<2)|doStuff()))

1. I don't know if its a type, should it be || instead of |. Would it still return a boolean with |.?
ANS: Yes! Its a type, u can use |. Yes it will return boolean with |
2. Assuming it was a type and it actually meant ||, k&b says:

No, u no need to assume, keep as it is

if (x>3) is false, no point in looking at the rest of the expression. 'cos during runtime it is evaluated as

Yes! exactly.

3. which means, it solves the first operation from left to right and the result of it, it operates with the next one.

can someone shed some light on this?

S, it solvz first operation from left to right and the result, with that result proceed to next operator and the other operand. this is the way how it work.


With regards,
Sethu.
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys. It makes sense.
 
reply
    Bookmark Topic Watch Topic
  • New Topic