||, _lazy_or

“short circuit or” of Boolean expressions

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

b1 || b2 || …
_lazy_or(b1, b2, …)

Description

b1 || b2 || ... evaluates the Boolean expression b1 or b2 or ... by “short circuit evaluation”.

b1 || b2 || ... produces the same result as bool(b1 or b2 or ...), provided the latter call does not throw an error. The difference between these calls is as follows. The call bool(b1 or b2 or ...) evaluates all Boolean expressions before combining them logically via or.

_lazy_or(b1, b2, ...) is equal to b1 || b2 || ....

The result is TRUE if one of b1, b2, and so on evaluates to TRUE. “short circuit evaluation” is based on this fact: b1 || b2 || ... evaluates the arguments from left to right. The evaluation stops immediately if one argument evaluates to TRUE. In this case, _lazy_or returns TRUE without evaluating the remaining Boolean expressions. If none of the expressions b1, b2, and so on evaluates to TRUE, then all arguments are evaluated and the corresponding result FALSE or UNKNOWN is returned.

_lazy_or is also called “conditional or”.

If any of the considered Boolean expressions b1, b2, and so on cannot be evaluated to TRUE, FALSE, or UNKNOWN, then _lazy_or throws an error.

_lazy_or is used internally by the if, repeat, and while statements. For example, the statement if b1 or b2 then ... is equivalent to if b1 || b2 then ....

_lazy_or() returns FALSE.

Examples

Example 1

This example show the difference between short circuit evaluation and complete evaluation of Boolean conditions. For x = 0, the evaluation of leads to an error:

x := 0:
bool(x = 0 or sin(1/x) = 0)
Error: Division by zero. [_invert]

With “short circuit evaluation”, the expression is not evaluated. This avoids the previous error:

x = 0 || sin(1/x) = 0

delete x:

Example 2

The following statements do no lead to an error because if uses short circuit evaluation internally:

for x in [0, PI, 1/PI] do
  if x = 0 or sin(1/x) = 0 then
     print(x)
  end_if;
end_for:

delete x

Example 3

_lazy_or() can be called without parameters:

_lazy_or()

Parameters

b1, b2, …

Boolean expressions

Return Values

TRUE, FALSE, or UNKNOWN.

Overloaded By

b1, b2

See Also

MuPAD Functions