Short-Circuit AND - Logical AND with short-circuiting - MATLAB (original) (raw)
Main Content
Logical AND with short-circuiting
Syntax
Description
[expr1](#mw%5F543464a1-bc87-4c8f-8f59-063f73903fd2) && [expr2](#mw%5F543464a1-bc87-4c8f-8f59-063f73903fd2)
represents a logical AND
operation that employs Logical Short-Circuiting behavior. That is, expr2
is not evaluated if expr1
is logical 0
(false
). Each expression must evaluate to a scalar logical result.
Examples
Specify a logical statement where the second condition depends on the first. In the following statement, it doesn't make sense to evaluate the relation on the right if the divisor, b
, is zero.
b = 1; a = 20; x = (b ~= 0) && (a/b > 18.5)
The result is logical 1
(true
). However, if (b ~= 0)
evaluates to false
, MATLABĀ® assumes the entire expression to be false
and terminates its evaluation of the expression early.
Specify b = 0
and evaluate the same expression.
b = 0; x = (b ~= 0) && (a/b > 18.5)
The result is logical 0
(false
). The first statement evaluates to logical 0
(false
), so the expression short-circuits.
Create a structure with fields named 'File'
and 'Format'
.
S = struct('File',{'myGraph'},'Format',[])
S = struct with fields: File: 'myGraph' Format: []
Short-circuit expressions are useful in if
statements when you want multiple conditions to be true. The conditions can build on one another in such a way that it only makes sense to evaluate the second expression if the first expression is true.
Specify an if
statement that executes only when S
contains an empty field named 'Format'
.
if isfield(S,'Format') && isempty(S.Format) S.Format = '.png'; end S
S = struct with fields: File: 'myGraph' Format: '.png'
The first condition tests if 'Format'
is the name of a field in structure S
. The second statement then tests whether the Format
field is empty. The truth of the second condition depends on the first. The second condition can never be true if the first condition is not true. Since S
has an empty field named 'Format'
, the body statement executes and assigns S.Format
the value '.png'
.
Input Arguments
Logical expressions, specified as any valid MATLABĀ® expressions that evaluate to logical scalars.
Example: isvector(x) && isreal(x)
Example: (x > 1) && (x < 10)
Data Types: logical
More About
With logical short-circuiting, the evaluation of logical expressions can terminate early once the result becomes fully determined. Due to the properties of logical AND and OR, the result of a logical expression is sometimes fully determined before evaluating all of the conditions:
- The logical
and
operator returns logical0
(false
) if even a single condition in the expression is false. - The logical
or
operator returns logical1
(true
) if even a single condition in the expression is true.
When the evaluation of a logical expression terminates early by encountering one of these values, the expression is said to have short-circuited. Used properly, this technique enables you to perform complex comparisons efficiently in your code.
For example, in the expression A && B
, MATLAB does not evaluate condition B
at all if condition A
is false. Once it is determined that A
is false, the value of B
cannot change the outcome of the operation.
Tips
- When you use the element-wise
&
and|
operators in the context of anif
orwhile
loop expression (and only in that context), they use short-circuiting to evaluate expressions.
However, you should always use the&&
and||
operators to enable short-circuit evaluation. Using the&
and|
operators for short-circuiting can yield unexpected results when the expressions do not evaluate to logical scalars.
Extended Capabilities
Version History
Introduced before R2006a