Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the | and || or operators?

Tags:

operators

c#

php

I have always used || (two pipes) in OR expressions, both in C# and PHP. Occasionally I see a single pipe used: |. What is the difference between those two usages? Are there any caveats when using one over the other or are they interchangeable?

like image 327
GloryFish Avatar asked Aug 29 '08 21:08

GloryFish


People also ask

What is the difference between and || operator in C?

The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand. The conditional logical OR operator ||, also known as the "short−circuiting" logical OR operator, computes the logical OR of its operands.

What does the || operator do?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What is the difference between the && and || operators and the & and operators?

&& is used to perform and operation means if anyone of the expression/condition evaluates to false whole thing is false. || is used to perform or operation if anyone of the expression/condition evaluates to true whole thing becomes true.

Is and || the same?

In short, the difference between the two operators boils down to the difference between falsy and null/undefined . Where the logical or ( || ) operator takes the right operand in the case of a falsy value — which includes empty string, 0, false, NaN, etc.


1 Answers

Just like the & and && operator, the double Operator is a "short-circuit" operator.

For example:

if(condition1 || condition2 || condition3) 

If condition1 is true, condition 2 and 3 will NOT be checked.

if(condition1 | condition2 | condition3) 

This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.

There is one big caveat, NullReferences or similar problems. For example:

if(class != null && class.someVar < 20) 

If class is null, the if-statement will stop after class != null is false. If you only use &, it will try to check class.someVar and you get a nice NullReferenceException. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.

No one ever uses the single & or | operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The & operator does "run these 3 functions, and if one of them returns false, execute the else block", while the | does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.

There is a Second use of the | and & operator though: Bitwise Operations.

like image 91
Michael Stum Avatar answered Oct 11 '22 15:10

Michael Stum