Main Content

If

Select subsystem execution using logic similar to if-else statement

  • If block

Libraries:
Simulink / Ports & Subsystems

Description

The If block, along with If Action Subsystem blocks that contain an Action Port block, implements if-else logic to control subsystem execution. For an example that uses the If block, see Select Subsystem Execution.

Model ex_if_block

Examples

Limitations

The If block has the following limitations:

  • It does not support tunable parameters. Values for an if or elseif expression cannot be tuned during a simulation in normal or accelerator mode, or when running generated code.

    To implement tunable if-else expressions, tune the expression outside the If block. For example, use the Relational Operator block to evaluate the expression outside of the If block or add the tunable parameter as an input to the If block.

  • It does not support custom storage classes. See Organize Parameter Data into a Structure by Using Struct Storage Class (Embedded Coder).

  • The if expression and elseif expressions cannot accept certain operators, such as +, -, *, and /.

Ports

Input

expand all

Inputs u1,u2,...,un must have the same data type. The inputs cannot be of any user-defined type, such as an enumerated type.

The If block does not directly support fixed-point data types. However, you can use the Compare To Constant block to work around this limitation. See Support for Fixed-Point Data Type in Select Subsystem Execution.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean

Output

expand all

Outputs from the if, else, and elseif ports are action signals to If Action Subsystem blocks.

Parameters

expand all

1

Specify one input port.

integer

Specify the number of input ports. Block ports are labeled with a 'u' character followed by a number, 1,2,...,n, where n equals the number of inputs that you specify.

Programmatic Use

Block Parameter: NumInputs
Type: character vector
Values: '1' | integer in quotes
Default: '1'

When the if port and all elseif port expressions are false, the else port sends an action signal to execute the attached If Action Subsystem block.

on

Display else port.

off

Hide else port.

Programmatic Use

Block Parameter: ShowElse
Type: character vector
Values: 'on' | 'off'
Default: 'on'

Control zero-crossing detection.

on

Detect zero crossings.

off

Do not detect zero crossings.

Programmatic Use

Block Parameter: ZeroCross
Type: character vector
Values: 'on' | 'off'
Default: 'on'

Since R2025a

Select this parameter to ensure only if-else statements are included in the code generated from an If block.

on

Only if-else statements are included in the generated code. This option prevents inclusion of switch-case statements in the generated code.

off

When this parameter is off, the code generated from an If block might contain switch-case statements, which perform the same operations as the If block.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Block Parameter: EnsureIfElseCode
Type: character vector
Values: 'on' | 'off'
Default: 'off'

The If Action Subsystem block attached to the if port executes when the associated if expression evaluates to true.

u1 > 0

Specify sending an action signal on the output port when the input u1 is greater than 0.

logical expression

Note

Starting in R2025a, to edit the default if expression (u1 > 0), use the Expressions table. For a complex expression with multiple inputs and operators, select the if expression and edit it in the Expression editor box.

For versions prior to R2025a, this parameter appears as If expressions in the block dialog box. You can specify logical expressions using this parameter.

However, across all Simulink® versions, this expression appears on the If block adjacent to the if output port.

The expression can include only the operators <, <=, ==, ~=, >, >=, &, |, ~, () , and unary-minus. Operators such as +, -, *, /, and ^ are not allowed. The expression must not contain data type expressions, for example, int8(6), and must not reference workspace variables whose data type is anything other than double or single.

Programmatic Use

Block Parameter: IfExpression
Type: character vector
Values: 'u1 > 0' | logical expression in quotes
Default: 'u1 > 0'

The If Action Subsystem block attached to an elseif port executes when the associated elseif expression evaluates to true and all other if and elseif expressions are false.

empty

Logical expressions not specified.

list of logical expressions

Note

Starting in R2025a, to add and edit elseif expressions, use the Expressions table. Use the action buttons on the left of the table to add, delete, and move the expressions. The table allows you to write each elseif expression separately instead of in a comma-separated list and organize the expressions per your modeling requirements. For a complex expression with multiple inputs and operators, select the row containing that elseif expression and edit it in the Expression editor box.

For versions prior to R2025a, this parameter appears as Else expressions in the block dialog box. In those versions, specify a list of logical expressions separated by commas.

However, across all Simulink versions, the expressions appear on the If block below the if port and above the else port when you select the Show else condition check box.

Expressions can include only the operators <, <=, ==, ~=, >, >=, &, |, ~, (), and unary-minus. Operators such as +, -, *, /, and ^ are not allowed. The expressions must not contain data type expressions, for example, int8(6), and must not reference workspace variables whose data type is anything other than double or single.

Programmatic Use

Block Parameter: ElseIfExpressions
Type: character vector
Values: '' | list of logical expressions separated by commas placed in quotes
Default: ''

Block Characteristics

Data Types

Boolean | double | integer | single

Direct Feedthrough

yes

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

yes

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Version History

Introduced before R2006a

expand all