Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Difference between

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering what the difference was between,
i. | and | |
11. & and &&
Any help is greatly appreciated.
Thanks.
-Jay
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

1. & and &&
The '&' operator can take two boolean operands or two integer operands, and it will evaluate both operands.
The '&&' operator takes only boolean operands. If the first operand is false the result is false and it will not evaluate the second operand.
2. Same case is for '|' and '| |' operators.
If I have time I will give sample codes.
Vanitha
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
| | and && are short-circuit operators, meaning that if the evaluation of the entire expression is already determined after evaluating the left-hand operand, the right-hand operand is ignored.

In the first if statement above, only the left-hand operand is evaluated because the expression will return true regardless of how the right-hand operation evaluates. Thus, the j++ is never executed and the value of j remains at 10
In the second if statement, the short-circuit operator is not used and so both sides of the if expression are evaluated, even though the left-hand operand still dictates that the return value will be true regardless of what happens with the right-hand operand.
The same logic holds true for && -- if the left-hand operand returns false, then the right-hand operand will not be evaluated.
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vanita and Scott.
Just to clear my doubt, does
i. (false | | true) returns false
(Since only left side operand is evaluated)
and
ii. (false | true) return true
(Since both operands are evaluated)
Thanks.
-Jay
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay, the short-circuit operators only short-circuit if evaluation of the left-hand operand returns a result (true for | | or false for &&) that cannot be changed by the right-hand operand.
Your first example


(false | | true) returns false
(Since only left side operand is evaluated)


is incorrect because the left-hand operand does not dictate the return value of the entire expression -- the right-hand operand will determine what the return value is. The short-circuit for | | kicks in only if the left-hand operand returns true.
| vs. | | will never return different values, and neither will & vs. &&

[This message has been edited by Scott Appleton (edited May 30, 2001).]
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So & and && (or | or | |) do the same except that && skips
evaluating right hand operand if the left hand operand gives the
result.
Thanks Scott.
-Jay
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

in the first case, we evaluate both left and right expression even though we know the result after evaluating left value. "getBoolean() invoked" gets printed.
when evaluating b2, the getBoolean() method wouldn't be executed because we already know the result of "or" expression after evaluating left value which is true. Nothing gets printed.
Hope this helps.
[This message has been edited by Andy Skalkin (edited May 30, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy's example is very clear.
--------------------------------------
CASE 1:
static boolean getBoolean() {
System. out.println("getBoolean() invoked");
return false;}boolean b1 = (true|getBoolean()); // 1st case
boolean b2 = (true| |getBoolean()); // 2nd case
}
As he explained "getBoolean() invoked" will be printed only one.
----------------------------------------------
CASE 2:
static boolean getBoolean() {
System. out.println("getBoolean() invoked");
return false;}boolean b1 = (true & getBoolean()); // 1st case
boolean b2 = (true && getBoolean()); // 2nd case
}
"getBoolean invoked" will be printed 2 times.
Reason:
& operator will evaluate both operands.
In this case && operator will evaluate both operands since evaluting only the left operand is not suffieient to produce the result.
--------------------------------------------------------
CASE 3:
static boolean getBoolean() {
System. out.println("getBoolean() invoked");
return false;}
boolean b1 = (false & getBoolean()); // 1st case
boolean b2 = (false && getBoolean()); // 2nd case
}
In this case "getBoolean() invoked " will be printed only once.
Reason:
&& operator will not evaluate the second operand because the first operand is false and it is suffient to produce the result and there is no need to evaluate the second operand.
------------------------------------------------------
AND and OR tables.
AND
false AND false --> false
false AND true --> false
true AND false --> false
true AND true ---> true
OR
false OR false --> false
false OR true --> true
true OR false --> true
true OR true --> true
I hope this helps.
Vanitha
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys for spending time and explaining the
concepts.
-Jay
    Bookmark Topic Watch Topic
  • New Topic