Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple If statement syntax

 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Just wondering...Is this a legal if statement:
[code]
if(hand<50 & !b) hand++;
[code]
Don't we need to have '&&' instead of '&' here.Please answer if you know this.Thanks
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

-Jeff-
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is perfectly legal to do a "bitwise and" operation with two boolean values, and the result is also a boolean.

Henry
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan McGuire
Ranch Hand
Posts: 1062
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been waiting YEARS for someone to ask this.

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.

However,...

...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.

Ryan
 
Ryan McGuire
Ranch Hand
Posts: 1062
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ryan
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic