That is a legal if statement. You're using the non-short-circuited boolean operator, which means that both sides of the & always get executed, regardless of the result of evaluating the left hand side.
Hi jas, in Java there are short circuit logical operators &&, || and non short circuit operators like &, |. The non short circuit operators ALWAYS evaluate both sides of the expression (though they are inefficient) while the short circuit operators don't waste time evaluating useless part of the expression. An example will make everything clear: a = 0; b = 1; c = d = 2; if (a==b && c==d)... at runtime this is what happens: a is equal to b ? NO...the entire expression is false, because there is AND (&&) so don't even bother evaluating the second part of the expression (c==d) because the result is FALSE regardless of the value of the second expression
If you had & instead of &&: a = 0; b = 1; c = d = 2; if (a==b && c==d)... at runtime this is what happens: a is equal to b ? NO...LET ME CHECK the other expression anyway...c is equal to d ? YES...but FALSE AND TRUE is FALSE...I wasted my time evaluating the second expression even if its value would have not changed the FINAL result! That's why non circuit operator are inefficient.
The same thing happens with || and |, with || if the first expression is TRUE, the FINAL result is TRUE, regardless of the value of the second expression; but if you use |, then even if the first expression is TRUE, the second expression is still being evaluated.
After this preface, yes...the code you wrote is correct and both hand<50 AND !b are evaluated.
I can provide you some practical examples of why you should use short circuit operators, but I don't want you to get confused.
Let me know if this explanation is enough! Giovanni
When used between boolean operands, & and | act just like && and ||, respectively, except that the single-symbol ones will always evaluate both operaands.
So something like...
...will print... Before: a=2, b=3 After: a=2, b=3
...becasue once the a>5 evaluates to false, the b++>5 isn't evaluated.
...will print... Before: a=2, b=3 After: a=2, b=4
Even though a>5 is false, the single & causes the b++>5 to be checked.
Joined: Feb 18, 2005
DAMN! Three people answered while I was typing MY response.
Henry, be careful calling & and | between boolean operands "bitwise operators". That fails to point out the whole non-short-circuit aspect of them.
Joined: May 14, 2003
Originally posted by Giovanni De Stefano: I can provide you some practical examples of why you should use short circuit operators, but I don't want you to get confused.
And in fact, you should prefer the use of short-circuited operators. The deliberate use of non-short-circuited operators means that you require the right-hand side to always be evaluated. That's potentially a design flaw--see http://c2.com/cgi/wiki?CommandQuerySeparation for a bit more information. There may be some cases (see the note about Kevlin's article) where you're forced to use non-short-circuited operators, but in general you should avoid them.
-Jeff- [ April 29, 2005: Message edited by: Jeff Langr ]
Joined: Jan 16, 2005
Thanks EverBody, I really appreciate to get so much response in a matter of few seconds.And I would really like to thank you all guys.