-1

Many use the notation below in scripting languages, but I don't quite understand how it works.

~$ false || echo "aa"
aa
~$ true || echo "aa"
~$ true && echo "aa"
aa
~$ false && echo "aa"
~$ 

Are both sides executed first, and then their return values evaluated?

What would be a more straight forward way to write these?

Is || an XOR?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162

4 Answers4

1

This is called Short Circuit Evaluation.

|| is an OR (not an XOR).

With scripting (as with source code) the right-side is only evaluated if necessary.

with || the RHS is evaluated if the LHS evaluates to false or 0. With && the RHS evaluates only if the LHS evaluates to true.

That is because true || anything is always true so there is no need to evaluate the RHS. And false && anything is always false.

See also Benefits of using short-circuit evaluation

Community
  • 1
  • 1
CashCow
  • 30,981
  • 5
  • 61
  • 92
1

|| and && are lazy in all the languages you listed.

The way it works is that the expression is evaluated left to right. In the case of ||, the value of the entire expression is the value of first argument which is not "false-y" (or the last argument, if all of the arguments are false-y) will be the value of the expression.

tom
  • 18,953
  • 4
  • 35
  • 35
1

I think this is pretty simple.

The left side is evaluated first every time and the right side is evaluated if required.

In the first case, LHS is false, so the result of the expression depends on the RHS as it is an OR operation. So the right side is evaluated/executed.

In the second case, LHS is true, and the result of the OR operation will be true regardless of what is there in the RHS, so the RHS is never evaluated/executed.

Same thing happens with the AND operation. It needs to evaluate the RHS only if LHS is true as in the 3rd statement.

Dipto
  • 2,720
  • 2
  • 22
  • 42
1

Using && and || to control the evaluation is a very fundamental thing in programming. As many already answered, they are using "Short Circuit Evaluation".
In some languages they are the same as and and or, but not in all. In e.g. Ruby they do not mean exactly the same (as I know).

What would be a more straight forward way to write these?

You could say that using && and || instead of if ... else is lazy programming, but some people would say that using && and || structure is the most straight forward.

If you will have an alternative (or a way of understand this better):

expr && echo "aa"

# is the same as

if expr; then
  echo "aa"
fi

and

expr || echo "aa"

# is the same as

if ! expr; then
  echo "aa"
fi

and

expr && echo "yes" || echo "no"

# is the same as

if expr; then
  echo "yes"
else
  echo "no"
fi

and of course a mix

if this && that; then
  echo "yes"
else
  echo "no"
fi

The "Short Circuit Evaluation" is a very important rule, and in some situations the code is depending on this.
In this example (from VIM, but it's the same in many languages) there would be an exception if it doesn't work:

if exists("variable") && variable == 3 ...

If both sides was evaluated before deciding the result there would be an exception if variable isn't defined.

There have been some bad designed languages where both sides of e.g. && was evaluated before the result was given, they were not so fun to use.

So:

if you like it; then
  use it && be happy
else
  use it || be happy
fi
244an
  • 1,579
  • 11
  • 15