Simulink Reference | ![]() ![]() |
Implement a C-like while
or do-while
control flow statement as a While subsystem
Library
Ports & Subsystems/While Subsystem
Description
The While Iterator block, when placed in a subsystem, implements a C-like while
or do-while
control flow statement in Simulink as a While subsystem. It has iterative control over any accompanying Simulink block programming placed in the same subsystem with it.
For each iteration of the While Iterator block, the accompanying blocks perform one execution. Iteration takes place as long as the input conditions are true. This applies to an initial condition for the first execution (input port labeled IC
) and a condition for succeeding executions (input port labeled cond
).
The following While subsystem example increments an initial value of 0
by 10
for every execution.
In the preceding example, a subsystem with a While block receives an input, which it passes to the IC (initial condition) port of the While block inside. If this value is true, the While block executes the blocks of the subsystem it is in. Since this value is 1
(true), the blocks execute and a value of 10
is added to a sum, which is initially 0
. The sum is then compared to a value of 100
. If the sum is less than or equal to 100
, a value of true is passed to the While block through the cond (condition) port. This causes the blocks to execute again and again until the value passed to the While Iterator block is false and execution ceases.
In addition, for each time the blocks of the subsystem execute, the While block outputs a value equal to the number of times that the blocks have executed, including the current execution. This value, along with the sum value, is sent to the x and y coordinate inputs, respectively, of an XY Graph block with the following result.
The preceding while
control flow statement example can be represented by the following pseudocode.
sum = 0; IC = 1; iteration_number = 0; cond = IC; while (cond != 0) { iteration_number = iteration_number + 1; sum = sum + sum_increment; if (sum >= 100 OR iterations > max_iterations) cond = 0; }
You construct a Simulink While subsystem like the preceding example as follows:
while{...}
.
You can use an ordinary subsystem or an atomic subsystem. In either case, the resulting While subsystem is atomic.
IC
) data input that controls the first execution must be outside the While subsystem.
You can set this field to a positive value to limit iterations and avoid infinite loops, or you can set this property to -1
if you want the While Iterator block to iterate without limit until an iteration condition (IC or cond port) is false (=0
). This is done in the preceding example.
This port outputs 1
for the first execution, which is incremented by 1
for each succeeding execution. If you select the Show iterations number port check box the Output data type field is enabled (it is grayed out otherwise). This allows you to select the type for the iteration number data output. The default type is int32
.
do-while
iteration instead of a while
iteration by selecting do-while for the While loop type field.
The equivalent pseudocode for a do-while
iteration in the preceding example (minus the IC port) is as follows:
sum = 0; iteration_number = 0; cond = 1; do { iteration_number = iteration_number + 1; sum = sum + sum_increment; if (sum >= 100 AND iterations > max_iterations) cond = 0; } while (cond);
When you change the While Iterator block to do-while
operation, the IC (initial condition) input disappears from the While Iterator block. The important distinction between the while
and do-while
modes of the While Iterator block is that the do-while
mode runs the While subsystem at least once. In while
mode, the While subsystem might not run its blocks at all, depending on the value of the initial condition (IC).
reset
(the default value), every time the While subsystem is called, its states are reset to their initial values. If you set the States when starting field to held
, the states of the While subsystem are retained between calls. In the preceding example, the While subsystem is called only once.
Data Type Support
Acceptable data inputs for the condition ports are any type except int64
and uint64
that includes a 0
value (includes fixed-point data).
You can select the data output type for the iterator output port in the parameter dialog box of the While block as double
, int32
, int16
, or int8
.
Parameters and Dialog Box
max_iterations
. If you set this value to -1
, the resulting While subsystem iterates as long as the input conditions (IC and cond ports) allow.while
or a do-while
operation for the While subsystem.reset
if you want the variables of the While subsystem to be reinitialized for each iteration. Otherwise, set this field to held
(the default) to make sure that the While subsystem states retain their values from one call to another.1
and is incremented by 1 for each succeeding iteration.int32
, int16
, int8
, or double
.Characteristics
Direct Feedthrough |
No |
Sample Time |
Inherited from driving block |
Scalar Expansion |
No |
Dimensionalized |
No |
Zero Crossing |
No |
![]() | Variable Transport Delay | While Iterator Subsystem | ![]() |