# Logical Operators: Short-Circuit && ||

Logical operations with short-circuiting

## Syntax

``expr1 && expr2``
``expr1 || expr2``

## Description

example

````expr1 && expr2` represents a logical `AND` operation that employs short-circuiting behavior. That is, `expr2` is not evaluated if `expr1` is logical `0` (`false`). Each expression must evaluate to a scalar logical result.```

example

````expr1 || expr2` represents a logical `OR` operation that employs short-circuiting behavior. That is, `expr2` is not evaluated if `expr1` is logical `1` (`true`). Each expression must evaluate to a scalar logical result.```

## Examples

collapse all

Create two vectors.

```X = [1 0 0 1 1]; Y = [0 0 0 0 0];```

Using the short-circuit OR operator with `X` and `Y` returns an error. The short-circuit operators operate only with scalar logical conditions.

Use the `any` and `all` functions to reduce each vector to a single logical condition.

`any(X) || all(Y)`
```ans = logical 1 ```

The expression is equivalent to `1 OR 0`, so it evaluates to logical `1` (`true`) after computing only the first condition, `any(X)`.

Specify a logical statement where the second condition depends on the first. In the following statement, it doesn't make sense to evaluate the relation on the right if the divisor, `b`, is zero.

```b = 1; a = 20; x = (b ~= 0) && (a/b > 18.5)```
```x = logical 1 ```

The result is logical `1` (`true`). However, if `(b ~= 0)` evaluates to `false`, MATLAB® assumes the entire expression to be `false` and terminates its evaluation of the expression early.

Specify `b = 0` and evaluate the same expression.

```b = 0; x = (b ~= 0) && (a/b > 18.5)```
```x = logical 0 ```

The result is logical `0` (`false`). The first statement evaluates to logical `0` (`false`), so the expression short-circuits.

Create a structure with fields named `'File'` and `'Format'`.

`S = struct('File',{'myGraph'},'Format',[])`
```S = struct with fields: File: 'myGraph' Format: [] ```

Short-circuit expressions are useful in `if` statements when you want multiple conditions to be true. The conditions can build on one another in such a way that it only makes sense to evaluate the second expression if the first expression is true.

Specify an `if` statement that executes only when `S` contains an empty field named `'Format'`.

```if isfield(S,'Format') && isempty(S.Format) S.Format = '.png'; end S```
```S = struct with fields: File: 'myGraph' Format: '.png' ```

The first condition tests if `'Format'` is the name of a field in structure `S`. The second statement then tests whether the `Format` field is empty. The truth of the second condition depends on the first. The second condition can never be true if the first condition is not true. Since `S` has an empty field named `'Format'`, the body statement executes and assigns `S.Format` the value `'.png'`.

collapse all

### Logical Short-Circuiting

With logical short-circuiting, the second operand, `expr2`, is evaluated only when the result is not fully determined by the first operand, `expr1`.

Due to the properties of logical AND and OR, the result of a logical expression is sometimes fully determined before evaluating all of the conditions. The logical `and` operator returns logical `0` (`false`) if even a single condition in the expression is false. The logical `or` operator returns logical `1` (`true`) if even a single condition in the expression is true. When the evaluation of a logical expression terminates early by encountering one of these values, the expression is said to have short-circuited.

For example, in the expression `A && B`, MATLAB® does not evaluate condition `B` at all if condition `A` is false. If `A` is false, then the value of `B` does not change the outcome of the operation.

When you use the element-wise `&` and `|` operators in the context of an `if` or `while` loop expression (and only in that context), they use short-circuiting to evaluate expressions.

Note

Always use the `&&` and `||` operators to enable short-circuit evaluation. Using the `&` and `|` operators for short-circuiting can yield unexpected results when the expressions do not evaluate to logical scalars.