Logical Operators, || or Or

Logical Operators, || or OR?

There is no "better" but the more common one is ||. They have different precedence and || would work like one would expect normally.

See also: Logical operators (the following example is taken from there):

// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;

What's the difference between or and | in ruby?

The | operator is a binary mathematical operator, that is it does a binary OR and works on a numerical level:

1 | 2
# => 3
4 | 3
# => 7
1 | 2 | 3
# => 3

This is because it's manipulating individual values as if they were binary:

0b01 | 0b10
# => 3 (0b11)

The || operator is a logical one, that is it returns the first value that's logically true. In Ruby only literal nil and false values evaluates as logically false, everything else, including 0, empty strings and arrays, is true.

So:

1 || 2
# => 1
0 || 1
# => 0

The or operator works almost exactly the same as || except it's at a much lower precedence. That means other operators are evaluated first which can lead to some problems if you're not anticipating this:

a = false || true
# => true
a
# => true

a = false or true
# => true
a
# => false

This is because it's actually interpreted as:

(a = false) or true

This is because = has a higher precedence when being evaluated.

difference between logical operators & and |

(a | b | c) == 0 does NOT mean "a is zero or b is zero or c is zero". For that you'd need (a==0 | b==0 | c==0) (you could also use ||, which stops evaluating after one operand is true).

With numeric operands, | is a bitwise "OR" operator, which you can read about separately. || is not valid for anything other than boolean operands, so it can be useful to be in the habit of using || unless you explicitly don;t want short-circuiting. That way you don't ever accidentally use it as a bitwise operator when you indented to use it as a logical operator. (it would have been a compiler error in this instance, for example).

Logical operators' precedence in C

There are three issues here:

  1. Order of precedence.
  2. Order of evaluation.
  3. Short circuiting of logical operators.

Order of precedence implies that ++a || ++b && ++c is evaluated as ++a || (++b && ++c).

However, due to the short circuiting requirements of logical operators, ++a is evaluated first. Only if that evaluates to false will (++b && ++c) be evaluated. In your case, ++a evaluates to true. Hence, (++b && ++c) is never evaluated.

swift Logical Operators orderly

It's called Short-circuit Evaluation and it means when a statement stops evaluation as soon as an outcome is determined. The parts of an expression containing && or || operators are evaluated only until it’s known whether the condition is true or false. This speeds up the execution of expression evaluation.

So in your code true || addOne() && addOne() && addOne() is known to be true as soon as you find true in your expression. In general true || whatever will always be true, so there is no need to evaluate whatever.

&& has precedence over || so :

true || addOne() && addOne() && addOne()

is equivalent to :

true || (addOne() && addOne() && addOne())

And we already know that true OR whatever is true, so whatever won't be evaluated.

To change the default precedence in your expression use parentheses where you see fit. For example:

(true || addOne()) && (addOne() && addOne())

In this case, the first addOne() won't be evaluated, But the second and third will be, since true AND something is something. And thus, in this case, x would be equal to 2 (supposing that initially x = 0).

Here is a final example (I suppose you get it by now, if not let me know in the comments):

if (true || addOne() && addOne())   &&   addOne()

In this case, there is an OR between true and addOne() && addOne(). Without evaluating addOne() && addOne() we already know that true || addOne() && addOne() is true. So the expression could be simplified to true && addOne(). Here we have to evaluate addOne(), which means, x will equal 1.


Edit

Swift operators belong to Precedence Groups (or levels) which are used to decide which operation has more priority in the evaluation of an expression. A higher precedence level means more priority.

The logical AND && belongs to the LogicalConjunctionPrecedence group, which has a higher precedence than the LogicalDisjunctionPrecedence group, to which the logical OR || belongs to. And thus && has more precedence than ||.

To learn more about operator precedence groups/levels, have a look right at the bottom of this, or that.

Why do we usually use || over |? What is the difference?

If you use the || and && forms, rather than the | and & forms of these operators, Java will not bother to evaluate the right-hand operand alone.

It's a matter of if you want to short-circuit the evaluation or not -- most of the time you want to.

A good way to illustrate the benefits of short-circuiting would be to consider the following example.

Boolean b = true;
if(b || foo.timeConsumingCall())
{
//we entered without calling timeConsumingCall()
}

Another benefit, as Jeremy and Peter mentioned, for short-circuiting is the null reference check:

if(string != null && string.isEmpty())
{
//we check for string being null before calling isEmpty()
}

more info

Efficiency of logical or on bool values in C++

Efficiency depends, the logical or operator || is a short circuit operator
meaning if x in your example is true it will not evaluate y or z.
If it was a logical and && then if x is false, it will not test y or z.
Its important to note that this operation does not exist as an instruction
so that means you have to use test and jump instructions. This means branching, which slows down things. Since modern CPU's are pipelined.

But the real answer is it depends, like many other questions of this nature, as sometimes the benefit of short circuiting operations outweighs the cost.

In the following extremely simple example you can see that bitwise or | is superior.

#include <iostream>


bool test1(bool a, bool b, bool c)
{
return a | b | c;
}

bool test2(bool a, bool b, bool c)
{
return a || b || c;
}

int main()
{
bool a = true;
bool b = false;
bool c = true;
test1(a,b,c);
test2(a,b,c);
return 0;
}

The following is the intel-style assembly listings produced by gcc-4.8 with -O3 :

test1 assembly :

_Z5test1bbb:
.LFB1264:
.cfi_startproc
mov eax, edx
or eax, esi
or eax, edi
ret
.cfi_endproc

test2 assembly :

_Z5test2bbb:
.LFB1265:
.cfi_startproc
test dil, dil
jne .L6
test sil, sil
mov eax, edx
jne .L6
rep; ret
.p2align 4,,10
.p2align 3
.L6:
mov eax, 1
ret
.cfi_endproc

You can see that it has branch instructions, which mess up the pipeline.

Sometimes however short-circuiting is worth it such as

return x && deep_recursion_function();

Disclaimer:

I would always use logical operators on bools. Unless performance really is critical, or maybe simple case like in test1 and test2 but with lots of bools.
And in either case first verify that you do get an improvement.

Why doesnt the OR logical operator( || ) work?

Logical OR (||) means "if either of these is true." In your case, if the input is not 'E' or it's not 'e' then it will perform another iteration. This is always true since even if it's one of them it's not going to be the other.

You're probably thinking of logical AND (&&):

while (ansr != 'E' && ansr != 'e');

This means "if both of these are true." If ansr is either 'E' or 'e', one of the clauses will be false, and the whole expression will then become false.



Related Topics



Leave a reply



Submit