|
Logical Operators: Short-Circuit && ||
Logical operations with short-circuiting
Syntax
expr1 && expr2
expr1 || expr2
Description
expr1 && expr2 represents a logical AND operation that employs short-circuiting
behavior on page 1-4. That is, expr2 is not evaluated if expr1 is logical 0 (false).
Each expression must evaluate to a scalar logical result.
expr1 || expr2 represents a logical OR operation that employs short-circuiting
behavior on page 1-4. That is, expr2 is not evaluated if expr1 is logical 1 (true). Each
expression must evaluate to a scalar logical result.
Examples
Use Scalar Logical Conditions
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) || any(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).
|
|