Simulink Reference    
While Iterator

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.

You construct a Simulink While subsystem like the preceding example as follows:

  1. Place a While Iterator block in a subsystem.
  1. This changes the subsystem icon to while{...}.

    You can use an ordinary subsystem or an atomic subsystem. In either case, the resulting While subsystem is atomic.

  1. The source for the initial condition (labeled IC) data input that controls the first execution must be outside the While subsystem.
  2. Input to the cond port controls any succeeding executions and must originate from within the Simulink programming residing in the While subsystem.
  3. Open the Block Parameters dialog of the While block and enter as follows:

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

Maximum number of iterations
The maximum number of iterations allowed. In the pseudocode examples, this value is represented as 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 loop type
Specifies while or a do-while operation for the While subsystem.
States when starting
Set this field to 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.
Show iteration number port
If this check box is selected (the default), the While Iterator block outputs its iteration value. This value starts at 1 and is incremented by 1 for each succeeding iteration.
Output data type
If the Show iteration number port check box is selected (the default), this field is enabled. It sets the data type of the iteration number output to 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