| Using Simulink | ![]() |
Modeling with Control Flow Blocks
The control flow blocks are used to implement the logic of the following C-like control flow statements in Simulink:
Although all the preceding control flow statements are implementable in Stateflow®, these blocks are intended to provide Simulink users with tools that meet their needs for simpler logical requirements.
Creating Conditional Control Flow Statements
You create C-like conditional control flow statements using ordinary subsystems and the following blocks from the Subsystems library.
| C Statement |
Blocks Used |
if-else |
If, Action Port |
switch |
Switch Case, Action Port |
If-Else Control Flow Statements
The following diagram depicts a generalized if-else control flow statement implementation in Simulink.
Construct a Simulink if-else control flow statement as follows:
Inputs to the If block are set in the If block properties dialog. Internally, they are designated as u1, u2,..., un and are used to construct output conditions.
Output ports for the If block are also set in its properties dialog. You use the input values u1, u2, ..., un to express conditions for the if, elseif, and else condition fields in the dialog. Of these, only the if field is required. You can enter multiple elseif conditions and select a check box to enable the else condition.
Each if, elseif, and else condition output port on the If block is connected to a subsystem to be executed if the port's case is true. You create these subsystems by placing an Action Port block in a subsystem. This creates an atomic Action subsystem with a port named Action, which you then connect to a condition on the If block. Once connected, the subsystem takes on the identity of the condition it is connected to and behaves like an enabled subsystem.
For more detailed information, see the reference topics for the If and Action Port blocks.
| Note All blocks in an Action subsystem driven by an If or Switch Case block must run at the same rate as the driving block. |
Switch Control Flow Statements
The following diagram depicts a generalized switch control flow statement implementation in Simulink.

Construct a Simulink switch control flow statement as follows:
The input to the Switch Case block is the argument to the switch control flow statement. This value determines the appropriate case to execute. Noninteger inputs to this port are truncated.
You add cases to the Switch Case block through the properties dialog of the Switch Case block. Cases can be single or multivalued. You can also add an optional default case, which is true if no other cases are true. Once added, these cases appear as output ports on the Switch Case block.
Each case output of the Switch Case block is connected to a subsystem to be executed if the port's case is true. You create these subsystems by placing an Action Port block in a subsystem. This creates an atomic subsystem with a port named Action, which you then connect to a condition on the Switch Case block. Once connected, the subsystem takes on the identity of the condition and behaves like an enabled subsystem. Place all the block programming executed for that case in this subsystem.
For more detailed information, see the reference topics for the Switch Case and Action Port blocks.
Creating Iterator Control Flow Statements
You create C-like iterator control flow statements using subsystems and the following blocks from the Subsystems library.
| C Statement |
Blocks Used |
do-while |
While Iterator |
for |
For Iterator |
while |
While Iterator |
While Control Flow Statements
The following diagram depicts a generalized C-like while control flow statement implementation in Simulink.

In a Simulink while control flow statement, the While Iterator block iterates the contents of a While subsystem, an atomic subsystem. For each iteration of the While Iterator block, the block programming of the While subsystem executes one complete path through its blocks.
Construct a Simulink while control flow statement as follows:
The host subsystem becomes a while control flow statement as indicated by its new label, while {...}. These subsystems behave like triggered subsystems. This subsystem is host to the block programming you want to iterate with the While Iterator block.
The While Iterator block requires an initial condition data input (labeled IC) for its first iteration. This must originate outside the While subsystem. If this value is nonzero, the first iteration takes place.
Conditions for the remaining iterations are passed to the data input port labeled cond. Input for this port must originate inside the While subsystem.
The iterator value is 1 for the first iteration and is incremented by 1 for each succeeding iteration.
This changes the label of the host subsystem to do {...} while. With a do-while iteration, the While Iteration block no longer has an initial condition (IC) port, because all blocks in the subsystem are executed once before the condition port (labeled cond) is checked.
For specific details, see the reference topic for the While Iterator block.
For Control Flow Statements
The following diagram depicts a generalized for control flow statement implementation in Simulink.

In a Simulink for control flow statement, the For Iterator block iterates the contents of a For Iterator Subsystem, an atomic subsystem. For each iteration of the For Iterator block, the block programming of the For Iterator Subsystem executes one complete path through its blocks.
Construct a Simulink for control flow statement as follows:
Through the properties dialog of the For Iterator block you can set it to take input for the number of iterations through the port labeled N. This input must come from outside the For Iterator Subsystem.
You can also set the number of iterations directly in the properties dialog.
The iterator value is 1 for the first iteration and is incremented by 1 for each succeeding iteration.
The For Iterator block works well with the Assignment block to reassign values in a vector or matrix. This is demonstrated in the following example. Note the matrix dimensions in the data being passed.
The above example outputs the sin value of an input 2-by-5 matrix (2 rows, 5 columns) using a For subsystem containing an Assignment block. The process is as follows:
| Building Simulation Targets | Comparing Stateflow and Control Flow Statements | ![]() |
© 1994-2005 The MathWorks, Inc.