Documentation

Logical Operators: Short-Circuit && ||

Logical operations with short-circuiting

Syntax

expr1 && expr2
expr1 || expr2

Description

example

expr1 && expr2represents a logicalANDoperation that employsshort-circuiting behavior. That is,expr2is not evaluated ifexpr1is logical0(false). Each expression must evaluate to a scalar logical result.

example

expr1 || expr2represents a logicalORoperation that employsshort-circuiting behavior. That is,expr2is not evaluated ifexpr1is logical1(true). Each expression must evaluate to a scalar logical result.

Examples

collapse all

Create two vectors.

X = [1 0 0 1 1]; Y = [0 0 0 0 0];

Using the short-circuit OR operator withXandYreturns an error. The short-circuit operators operate only with scalar logical conditions.

Use theanyandallfunctions to reduce each vector to a single logical condition.

any(X) || any(Y)
ans =logical1

The expression is equivalent to1 OR 0, so it evaluates to logical1(true) after computing only the first condition,any(X).

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)
x =logical1

The result is logical1(true). However, if(b ~= 0)evaluates tofalse, MATLAB® assumes the entire expression to befalseand terminates its evaluation of the expression early.

Specifyb = 0and evaluate the same expression.

b = 0; x = (b ~= 0) && (a/b > 18.5)
x =logical0

The result is logical0(false). The first statement evaluates to logical0(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 inifstatements 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 anifstatement that executes only whenScontains an empty field named'Format'.

ifisfield(S,'Format') & & isempty (S.Format) S.Format ='.png';endS
S =struct with fields:File: 'myGraph' Format: '.png'

The first condition tests if'Format'is the name of a field in structureS. The second statement then tests whether theFormatfield 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. SinceShas an empty field named'Format', the body statement executes and assignsS.Formatthe value'.png'.

More About

collapse all

Logical Short-Circuiting

With logical short-circuiting, the second operand,expr2, is evaluated only when the result is not fully determined by the first operand,expr1.

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 logicalandoperator returns logical0(false) if even a single condition in the expression is false. The logicaloroperator 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 haveshort-circuited.

For example, in the expressionA && B, MATLAB®does not evaluate conditionBat all if conditionAis false. IfAis false, then the value ofBdoes not change the outcome of the operation.

When you use the element-wise&and|operators in the context of aniforwhileloop expression (and只有in that context), they use short-circuiting to evaluate expressions.

Note

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.

Was this topic helpful?