Main Content

While Loop

This example shows how to implement a while loop construct by using Simulink® blocks, Stateflow® Charts, and MATLAB® Function blocks.

C Construct

while(flag && (num_iter <= 100)
{
  flag = func ();
  num_iter ++;
}

Modeling Pattern for While Loop: While Iterator Subsystem block

One method for creating a while loop is to use a While Iterator Subsystem block from the Simulink > Ports & Subsystems library.

1. Open example model ex_while_loop_SL.

The model contains a While Iterator Subsystem block that repeats execution of the contents of the subsystem during a simulation time step.

Observe the following settings in the model:

  • The Constant block provides an initial condition to the While Iterator Subsystem. For the Constant block, the Constant value is 1 and the Output data type is boolean. The initial condition can be dependent on the input to the block.

  • In the While Iterator Subsystem, the func subsystem block has an output flag of 0 or 1 depending on the result of the algorithm in func( ). func() is the Function name in func subsystem.

  • In the While Iterator Subsystem, for the While Iterator block, the Maximum number of iterations is 100.

  • For the While Iterator block, the While loop type is while.

2. To build the model and generate code, press Ctrl+B.

The code implementing the while loop is in the ex_while_loop_SL_step function in ex_while_loop_SL.c:

/* Model step function */
void ex_while_loop_SL_step(void)
{
  int32_T s1_iter;
  boolean_T loopCond;

  /* Outputs for Iterator SubSystem: '<Root>/While Iterator Subsystem' incorporates:
   *  WhileIterator: '<S1>/While Iterator'
   */
  s1_iter = 1;

  /* SystemReset for Atomic SubSystem: '<S1>/func' */
  func_Reset();

  /* End of SystemReset for SubSystem: '<S1>/func' */
  loopCond = true;
  while (loopCond && (s1_iter <= 100)) {
    /* Outputs for Atomic SubSystem: '<S1>/func' */
    func();

    /* End of Outputs for SubSystem: '<S1>/func' */
    loopCond = flag;
    s1_iter++;
  }

  /* End of Outputs for SubSystem: '<Root>/While Iterator Subsystem' */
}

Modeling Pattern for While Loop: Stateflow Chart

1. Open example model ex_while_loop_SF.

In the model, the ex_while_loop_SF/Chart executes the while loop.

The chart contains a While loop decision pattern that you add by right clicking inside the chart > Add Pattern in Chart > Loop > While.

2. To build the model and generate code, press Ctrl+B.

The code implementing the while loop is in the ex_while_loop_SF_step function in ex_while_loop_SF.c:

/* Model step function */
void ex_while_loop_SF_step(void)
{
  /* Chart: '<Root>/Chart' */
  num_iter = 1;
  while (flag && (num_iter <= 100)) {
    /* Outputs for Function Call SubSystem: '<Root>/func' */
    func();

    /* End of Outputs for SubSystem: '<Root>/func' */
    num_iter++;
  }

  /* End of Chart: '<Root>/Chart' */
}

Modeling Pattern for While Loop: MATLAB Function block

1. Open example model ex_while_loop_ML.

The MATLAB Function Block contains this function:

function fcn(func_flag)

flag = true; 
num_iter = 1;

while(flag && (num_iter<=100))
    func;
    flag = func_flag;
    num_iter = num_iter + 1;
end

2. To build the model and generate code, press Ctrl+B.

The code implementing the while loop is in the ex_while_loop_ML_step function in ex_while_loop_ML.c:

/* Model step function */
void ex_while_loop_ML_step(void)
{
  int32_T num_iter;
  boolean_T flag;
  boolean_T func_flag_0;

  /* MATLAB Function: '<Root>/MATLAB Function' */
  func_flag_0 = func_flag;
  flag = true;
  num_iter = 1;
  while (flag && (num_iter <= 100)) {
    /* Outputs for Function Call SubSystem: '<Root>/func' */
    func();

    /* End of Outputs for SubSystem: '<Root>/func' */
    flag = func_flag_0;
    num_iter++;
  }

  /* End of MATLAB Function: '<Root>/MATLAB Function' */
}

See Also

Related Examples

More About