MATLAB Function Reference Previous page   Next Page
Logical Operators: Short-circuit && ||

Logical operations, with short-circuiting capability

Syntax

Description

The symbols && and || are the logical AND and OR operators used to evaluate logical expressions. Use && and || in the evaluation of compound expressions of the form

where expression_1 and expression_2 each evaluate to a scalar logical result.

The && and || operators support short-circuiting. This means that the second operand is evaluated only when the result is not fully determined by the first operand. See Short-Circuit Operators in the MATLAB documentation for a discussion on short-circuiting with && and ||.

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:

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

all, any, find, logical, xor, true, false

Logical operators, elementwise, &, |, ~

Relational operators <, <=, >, >=, ==, ~=


Previous page  Logical Operators: Elementwise & | ~ loglog Next page

© 1994-2005 The MathWorks, Inc.