`&&`, `_lazy_and`

“short circuit and” 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_and(`b1, b2, …`)
```

Description

`b1 && b2 && ...` evaluates the Boolean expression `b1 and b2 and ...` by “short circuit evaluation”.

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

`_lazy_and(b1, b2, ...)` is equal to ```b1 && b2 && ...```.

Note that the result is `FALSE` if one of `b1`, `b2`, and so on evaluates to `FALSE`. “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 `FALSE`. In this case, `_lazy_and` returns `FALSE` without evaluating the remaining Boolean expressions. If none of the expressions `b1`, `b2`, and so on evaluates to `FALSE`, then all arguments are evaluated and the corresponding result (`TRUE` or `UNKNOWN`) is returned.

`_lazy_and` is also called “conditional `and`”.

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

`_lazy_and` is used internally by the `if`, `repeat`, and `while` statements. For example, the statement ```if b1 and b2 then ...``` is equivalent to ```if b1 && b2 then ...```.

`_lazy_and()` returns `TRUE`.

Examples

Example 1

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

```x := 0: bool(x <> 0 and 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 and sin(1/x) = 0 then print(x) end_if; end_for:```

`delete x`

Example 3

`_lazy_and` can be called without parameters:

`_lazy_and()`

Parameters

 `b1, b2, …` Boolean expressions

Return Values

`TRUE`, `FALSE`, or `UNKNOWN`.

`b1`, `b2`