Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Logical Operators: Short-circuit && || - Logical operations, with short-circuiting capability

Syntax

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.

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

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

Logical Operators: Elementwise & | ~

Relational Operators < > <= >= == ~=

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS