| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
Equivalent C Language Statements |
You can use block diagrams to model control flow logic equivalent to the following C programming language statements:
for
if-else
switch
while
You can use the following blocks to model conditional control flow logic.
| C Statement | Equivalent Blocks |
|---|---|
if-else | |
switch |
The following diagram models if-else control flow.

Construct an if-else control flow diagram as follows:
Provide data inputs to the If block for constructing if-else conditions.
Inputs to the If block are set in the If block properties dialog box. Internally, they are designated as u1, u2,..., un and are used to construct output conditions.
Set output port if-else conditions for the If block.
Output ports for the If block are also set in its properties dialog box. You use the input values u1, u2, ..., un to express conditions for the if, elseif, and else condition fields in the dialog box. Of these, only the if field is required. You can enter multiple elseif conditions and select a check box to enable the else condition.
Connect each condition output port to an Action subsystem.
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 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. |
The following diagram models switch control flow.

Construct a switch control flow statement as follows:
Provide a data input to the argument input of the Switch Case block.
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.
Add cases to the Switch Case block based on the numeric value of the argument input.
You add cases to the Switch Case block through the properties dialog box 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.
Connect each Switch Case block case output port to an Action subsystem.
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 Simulink Reference for the Switch Case and Action Port blocks.
Note After the subsystem for a particular case is executed, an implied break is executed that exits the switch control flow statement altogether. The Simulink software switch control flow statement implementations do not exhibit "fall through" behavior like C switch statements. |
The following blocks allow you to model while and for loops.
| C Statement | Equivalent Blocks |
|---|---|
do-while | |
for | |
while |
The following diagram illustrates a while loop.

In this example, the Simulink software repeatedly executes the contents of the While subsystem at each time step until a condition specified by the While Iterator block is satisfied. In particular, for each iteration of the loop specified by the While Iterator block, the Simulink software invokes the update and output methods of all the blocks in the While subsystem in the same order that the methods would be invoked if they were in a noniterated atomic subsystem.
Note Simulation time does not advance during execution of a While subsystem's iterations. Nevertheless, blocks in a While subsystem treat each iteration as a time step. As a result, in a While subsystem, the output of a block with states, i.e., a block whose output depends on its previous input, reflects the value of its input at the previous iteration of the while loop—not, as one might expect, its input at the previous simulation time step. For example, a Unit Delay block in a While subsystem outputs the value of its input at the previous iteration of the while loop—not the value at the previous simulation time step. |
Construct a while loop as follows:
Place a While Iterator block in a subsystem.
The host subsystem's label changes to while {...} to indicate that it is modeling a while loop. These subsystems behave like triggered subsystems. This subsystem is host to the block programming you want to iterate with the While Iterator block.
Provide a data input for the initial condition data input port of 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.
Provide data input for the conditions port of the While Iterator block.
Conditions for the remaining iterations are passed to the data input port labeled cond. Input for this port must originate inside the While subsystem.
You can set the While Iterator block to output its iterator value through its properties dialog.
The iterator value is 1 for the first iteration and is incremented by 1 for each succeeding iteration.
You can change the iteration of the While Iterator block to do-while through its properties dialog.
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.
Create a block diagram in the subsystem that defines the subsystem's outputs.
For more information, see the While Iterator block.
The following diagram models a for loop:

In this example, the Simulink software executes the contents of the For subsystem multiples times at each time step with the number of iterations being specified by the input to the For Iterator block. In particular, for each iteration of the for loop, the Simulink software invokes the update and output methods of all the blocks in the For subsystem in the same order that the methods would be invoked if they were in a noniterated atomic subsystem.
Note Simulation time does not advance during execution of a For subsystem's iterations. Nevertheless, blocks in a For subsystem treat each iteration as a time step. As a result, in a For subsystem, the output of a block with states, i.e., a block whose output depends on its previous input, reflects the value of its input at the previous iteration of the for loop—not, as one might expect, its input at the previous simulation time step. For example, a Unit Delay block in a For subsystem outputs the value of its input at the previous iteration of the for loop—not the value at the previous simulation time step. |
Construct a for loop as follows:
Drag a For Iterator Subsystem block from the Library Browser or Library window into your model.
You can set the For Iterator block to take external or internal input for the number of iterations it executes.
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.
You can set the For Iterator block to output its iterator value for use in the block programming of the For Iterator Subsystem.
The iterator value is 1 for the first iteration and is incremented by 1 for each succeeding iteration.
Create a block diagram in the subsystem that defines the subsystem's outputs.
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 sine 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:
A 2-by-5 matrix is input to the Selector block and the Assignment block.
The Selector block strips off a 2-by-1 matrix from the input matrix at the column value indicated by the current iteration value of the For Iterator block.
The sine value 2-by-1 matrix is passed to an Assignment block.
The Assignment block, which takes the original 2-by-5 matrix as one of its inputs, assigns the 2-by-1 matrix back into the original matrix at the column location indicated by the iteration value.
The rows specified for reassignment in the property dialog for the Assignment block in the above example are [1,2]. Because there are only two rows in the original matrix, you could also have specified -1 for the rows, i.e., all rows.
Note Experienced Simulink software users will note that the Trigonometric Function block is already capable of taking the sine of a matrix. The above example uses the Trigonometric Function block only as an example of changing each element of a matrix with the collaboration of an Assignment block and a For Iterator block. |
![]() | Creating Subsystems | Using Callback Functions | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |