If

Model if-else control flow

Library

Ports & Subsystems

Description

The If block, along with the If Action Subsystem block containing an Action Port, implements standard C-like if-else logic.

The following shows a completed if-else control flow statement.

In this example, the inputs to the If block determine the values of conditions represented as output ports. Each output port is attached to an If Action Subsystem, named body_1, body_2, and body_3. The conditions are evaluated top down starting with the if condition. If a condition is true, its If Action Subsystem is executed and the If block does not evaluate any remaining conditions.

The preceding if-else control flow statement can be represented by the following pseudocode.

if (u1 > 0) {
	body_1;
}
else if (u2 > 0){
	body_2;
}
else {
	body_3;
}

You construct a Simulink® if-else control flow statement like the preceding example as follows:

  1. Place an If block in the current system.

  2. Open the dialog of the If block and enter as follows:

    • Enter the Number of inputs field with the required number of inputs necessary to define conditions for the if-else control flow statement.

      Elements of vector inputs can be accessed for conditions using (row, column) arguments. For example, you can specify the fifth element of the vector u2 in the condition u2(5) > 0 in an If expression or Elseif expressions field.

    • Enter the expression for the if condition of the if-else control flow statement in the If expression field.

      This creates an if output port for the If block with a label of the form if(condition). This is the only required If Action signal output for an If block.

    • Enter expressions for any elseif conditions of the if-else control flow statement in the Elseif expressions field.

      Use a comma to separate one condition from another. Entering these conditions creates an output port for the If block for each condition, with a label of the form elseif(condition). elseif ports are optional and not required for operation of the If block.

    • Check the Show else condition check box to create an else output port.

      The else port is optional and not required for the operation of the If block.

  3. Create If Action subsystems to connect to each of the if, else, and elseif ports.

    These consist of a subsystem with an Action Port block. When you place an Action Port block inside each subsystem, an input port named Action is added to the subsystem.

  4. Connect each if, else, and elseif port of the If block to the Action port of an If Action subsystem.

    When you make the connection, the icon for the If Action block is renamed to the type of the condition that it attaches to.

      Note   During simulation of an if-else control flow statement, the Action signal lines from the If block to the If Action subsystems turn from solid to dashed.

  5. In each If Action Subsystem, enter the Simulink blocks appropriate to the body to be executed for the condition it handles.

      Note:   All blocks in an If Action Subsystem must run at the same rate as the driving If block. You can achieve this by setting each block's sample time parameter to be either inherited (-1) or the same value as the If block's sample time.

Limitations

The If block has the following limitations:

  • You cannot tune an if or elseif expression during simulation in Normal or Accelerator mode (see How Acceleration Modes Work), or in generated code. The If block doesn't support tunable parameters if Inline Parameters is turned on. 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 or add the tunable parameter as an input to the If block.

  • The If block does not support custom storage classes. See Custom Storage Classes in the Embedded Coder® documentation.

  • The If expression and Elseif expressions cannot accept certain operators, such as +, -, *, and /. See If Expression and Elseif Expressions in Parameters and Dialog Box

Data Type Support

Inputs u1,u2,...,un can be scalars or vectors of any built-in Simulink data type and must all be of the same data type. The inputs cannot be of any user-defined type, such as an enumerated type. Outputs from the if, else, and elseif ports are Action signals to If Action subsystems that you create by using Action Port blocks and subsystems.

For more information, see Data Types Supported by Simulink in the Simulink documentation.

Parameters and Dialog Box

Number of inputs

Specify the number of inputs to the If block. These appear as data input ports labeled with a 'u' character followed by a number, 1,2,...,n, where n equals the number of inputs that you specify.

If expression

Specify the condition for the if output port. This condition appears on the If block adjacent to the if output port. The If expression can include only the operators <, <=, ==, ~=, >, >=, &, |, ~, (), unary-minus, and cannot include operators such as +, -, *, /, and ^. The If Action Subsystem attached to the if port executes if its condition is true. The expression must not contain data type expressions, for example, int8(6), and must not reference workspace variables whose data type is other than double or single.

    Note:   There are limitations to the tunability of the If expression. See Limitations for more information.

Elseif expressions

Specify a string list of elseif conditions delimited by commas. These conditions appear below the if port and above the else port when you select the Show else condition check box. Elseif expressions can include only the operators <, <=, ==, ~=, >, >=, &, |, ~, (), unary-minus, and cannot include operators such as +, -, *, /, and ^. The If Action Subsystem attached to an elseif port executes if its condition is true and all of the if and elseif conditions are false. The expression must not contain data type expressions, for example, int8(6), and must not reference workspace variables whose data type is other than double or single.

    Note:   There are limitations to the tunability of Elseif expressions. See Limitations for more information.

Show else condition

If you select this check box, an else port is created. The If Action subsystem attached to the else port executes if the if port and all the elseif ports are false.

Enable zero-crossing detection

Select to enable zero-crossing detection. For more information, see Zero-Crossing Detection.

Sample time

Specify the sample time of the input signal. See Specify Sample Time in the online documentation for more information.

Examples

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.

For example, consider the following floating-point modelfloating-point model:

In this model, the If Action subsystems use their default configurations. The block and simulation parameters for the model are set to their default values except as follows:

Block or Dialog Box

Parameter

Setting

Configuration Parameters — Solver pane

Start time

0.0

Stop time

1.0

Type

Fixed-step

Solver

discrete (no continuous states)

Fixed-step size

0.1

Repeating Sequence Stair

Vector of output values

[-2 -1 1 2].'

Repeating Sequence Stair1

Vector of output values

[0 0 0 0 1 1 1 1].'

If

Number of inputs

2

If expression

(u1 > 0) | (u2 > 0.5)

Show else condition

selected

Constant

Constant value

-4

Constant1

Constant value

4

Scope

Number of axes

3

Time range

1

For this model, if input u1 is greater than 0 or input u2 is greater than 0.5, the output is 4. Otherwise, the output is –4. The Scope block shows the output, u1, and u2:

You can implement this block diagram as a model with fixed-point data typesmodel with fixed-point data types:

The Repeating Sequence Stair blocks now output fixed-point data types.

The Compare To Constant blocks implement two parts of the If expression that is used in the If block in the floating-point version of the model, (u1 > 0) and (u2 > 0.5). The OR operation, (u1|u2), can still be implemented inside the If block. For a fixed-point model, the expression must be partially implemented outside of the If block as it is here.

The block and simulation parameters for the fixed-point model are the same as for the floating-point model with the following exceptions and additions:

Block

Parameter

Setting

Compare To Constant

Operator

>

 

Constant value

0

 

Output data type mode

Boolean

 

Enable zero-crossing detection

off

Compare To Constant1

Operator

>

 

Constant value

0.5

 

Output data type mode

Boolean

 

Enable zero-crossing detection

off

If

Number of inputs

2

 

If expression

u1|u2

Characteristics

Direct Feedthrough

Yes

Sample Time

Inherited from driving block

Scalar Expansion

No

Dimensionalized

Yes

Zero-Crossing Detection

Yes, if enabled

Was this topic helpful?