Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# Logical Operators: Short-circuit && ||

Logical operations, with short-circuiting capability

expr1 && expr2
expr1 || expr2

## Description

expr1 && expr2 represents a logical AND operation that employs short-circuiting behavior. With short-circuiting, the second operand expr2 is evaluated only when the result is not fully determined by the first operand expr1. For example, if A = 0, then the following statement evaluates to false, regardless of the value of B, so the MATLAB® software does not evaluate B:

`A && B`

These two expressions must each be a valid MATLAB statement that evaluates to a scalar logical result.

expr1 || expr2 represents a logical OR operation that employs short-circuiting behavior.

 Note   Always use the && and || operators when short-circuiting is required. Using the elementwise operators (& and |) for short-circuiting can yield unexpected results.

## Examples

In the following statement, it doesn't make sense to evaluate the relation on the right if the divisor, b, is zero. The test on the left is put in to avoid generating a warning under these circumstances:

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

By definition, if any operands of an AND expression are false, the entire expression must be false. So, if (b ~= 0) evaluates to false, MATLAB assumes the entire expression to be false and terminates its evaluation of the expression early. This avoids the warning that would be generated if MATLAB were to evaluate the operand on the right.

## See Also

Was this topic helpful?