Skip to Main Content Skip to Search
Product Documentation

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 | false | find | logical | Logical Operators: Elementwise & | ~ | Relational Operators < > <= >= == ~= | true | xor

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS