Main Content

Logic Operations Short-Circuiting

Logical expressions containing && (AND) and || (OR) operators exhibit short-circuiting.

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-circuited evaluation of logical expressions helps you to optimize the execution by avoiding unnecessary evaluations when the result is already determined.

Short-Circuiting Behavior in Condition and MCDC Objectives

When you generate tests or analyze for dead logic by using Simulink® Design Verifier™, the logical operations and expressions in Condition and MCDC objectives can exhibit short-circuiting. This table summarizes the short-circuiting behavior.

Short-circuit consideration in Condition and MCDC objectives:

Modeling elementShort-circuit consideration for Condition and MCDC Objectives

Logical Operator blocks (standalone/cascaded)

Condition objectives are always short circuited.

MCDC objectives are short-circuited only when the parameter CovLogicBlockShortCircuit is ON

Enabled or Triggered subsystems

Condition objectives are short-circuited. MCDC objectives are non short-circuited.

MATLAB®, Stateflow® (C or MATLAB) and other Simulink blocks (Fcn, If)

Condition and MCDC objectives are always short-circuited

The tables summarizes some examples for Condition objectives from various blocks:

Short-Circuit considerations for Condition objectives:

BlockCondition Objective: Short-circuit Expression

Logical AND block with 3 ports

Condition 'F', Port 2

(in1) &&(~ in2)

Logical OR block with 3 ports

Condition 'F', Port 2

(~in1) &&(~ in2)

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

'u2' False

(u1) && (~u2)

The tables summarizes the difference between MCDC objectives that depend on CovLogicBlockShortCircuit parameter:

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

BlockMCDC ObjectiveMCDC Expression 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)

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

expression for output with input variable 2 false

(in1 && ~in2)

(in1 && ~in2)

expression for output with input variable 2 true

(in1 && in2 && in3)

(in1 && in2 && in3)

Note

When you derive MCDC expression by using cascaded logic, it exhibits similar short-circuiting behavior as the Logical Operator 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 MCDC Result

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

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

BlockMCDC ObjectiveMCDC Expression 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 && in3) (Dead Logic)

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