Main Content

Logical Operations Short-circuiting

Short-circuit is exhibited by logical expressions containing && (AND) and || (OR) operators.

In a logical expression involving the && operator, if the left-hand side expression evaluates to false, the overall expression also evaluates to false. In this case, the right-hand side expression is not evaluated because the outcome is already determined by the left-hand side expression.

Similarly, for a logical expression involving the || operator, if the left-hand side expression evaluates to true, the right-hand side expression is not evaluated because the outcome is determined by the left-hand side expression. This short-circuit evaluation of logical expressions helps you to optimize the execution by avoiding unnecessary evaluations when the result is already determined.

Short-circuiting Behavior for Condition and MCDC Objectives

Logical Operators and Logical Expressions for Condition and MCDC objectives can be considered short-circuiting or not when you analyze for dead logic or generate tests. The table summarizes different considerations:

Short-circuit consideration for Condition and MCDC objectives:

Modeling elementShort-circuit consideration for Condition and MCDC Objectives

Logic blocks (standalone/cascaded)

Condition and MCDC objectives are short-circuited only when CovLogicBlockShortCircuit is ON

Enabled subsystem

Condition and MCDC objectives are always non short-circuited.

Triggered subsystem

Condition and MCDC objectives are always non short-circuited.

Enabled and Triggered subsystem

Condition and MCDC objectives are always non short-circuited.

MATLAB®, Stateflow® (C or MATLAB as action language) and other Simulink® Blocks (Fcn, If)

Condition and MCDC objectives always short-circuited

Short-circuiting Behavior for Condition Objectives

The table summarizes the difference between Condition objectives that depend on CovLogicBlockShortCircuit parameter:

Short-circuit considerations for Condition objectives based on CovLogicBlockShortCircuit parameter

BlockCondition Objective Condition Expression when CovLogicBlockShortCircuit is ONCondition Expression when CovLogicBlockShortCircuit is OFF

Logical AND block with 3 ports

Condition 'F', Port 2

(in1 && ~in2)

(~in2)

Logical OR block with 3 ports

Condition 'F', Port 2

(~in1 && ~in2)

(~in2)

MATLAB Function or Chart containing expression (u1 && u2 && u3)

'u2' False

(u1 && ~u2)

(u1 && ~u2)

Short-circuiting Behavior for MCDC Objectives

The table summarizes the difference in objectives that vary based on the CovLogicBlockShortCircuit parameter as shown:

Short-Circuit considerations for MCDC objectives based on CovLogicBlockShortCircuit parameter:

BlockMCDC ObjectiveMCDCExpression when CovLogicBlockShortCircuit is ONMCDC Expression when CovLogicBlockShortCircuit is OFF

Logical AND block with 3 ports

expression for output with input port 2 false

(in1 && ~in2)

(in1 && ~in2 && in3)

expression for output with input port 2 true

(in1 && in2 && in3)

(in1 && in2 && in3)

MATLAB Function or Chart containing expression (u1 && u2 && u3)

expression for output with input port 2 false

(in1 && ~in2)

(in1 && ~in2)

expression for output with input port 2 true

(in1 && in2 && in3)

(in1 && in2 && in2)

Note

When MCDC expression is derived from a cascaded logic, it follows similar definition as logic blocks.

For more information, see "Short-Circuiting of Boolean Expressions for MCDC" in Analyzing MCDC for Cascaded Logic Blocks (Simulink Coverage).

Impact of CovLogicBlockShortCircuit parameter on Condition and MCDC Results

In some cases, coverage result for MCDC objectives can vary based on the CovLogicBlockShortCircuit parameter as shown:

When CovLogicBlockShortCircuit parameter is OFF, all the inputs are considered.

Short-Circuit considerations for Condition objectives based on CovLogicBlockShortCircuit parameter:

BlockCondition ObjectiveCondition Expression when CovLogicBlockShortCircuit is ONCondition Expression when CovLogicBlockShortCircuit is OFF

Logical AND block with 3 ports

Condition 'F', Port 3

(in1 && in2 && ~in2) (Dead Logic)

(~in2) (Active Logic)

Short-Circuit considerations for MCDC objectives based on CovLogicBlockShortCircuit parameter:

BlockMCDC ObjectiveMCDCExpression when CovLogicBlockShortCircuit is ONMCDC Expression when CovLogicBlockShortCircuit is OFF

Logical AND block with 3 ports

Expression for output with input port 2 false

(in1 && ~in2) (Active Logic)

(in1 && ~in2 && in2) (Dead Logic)