%% Do While Loop
% This example shows how to implement a |do while| loop construct  
% by using Simulink blocks and Stateflow Charts.

% Copyright 2019 The MathWorks, Inc.

%% C Construct
% <include>c_construct_do_while.txt</include>

%% Modeling Pattern for Do While Loop: While Iterator Subsystem block
% One method for creating a |do while| loop is to use a While Iterator Subsystem 
% block from the *Simulink > Ports and Subsystems* library.
%
% 1. Open example model <matlab:ex_do_while_loop_SL |ex_do_while_loop_SL|>.
open_system('ex_do_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.
open_system('ex_do_while_loop_SL/While Iterator Subsystem')

%%
% Observe the following settings 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.
%
% * For the While Iterator block, the *Maximum number of iterations* is 
%   |100|.
% 
% * For the While Iterator block, the *While loop type* is |do-while|.
%
% 2. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_do_while_loop_SL'')');

%%
% The code implementing the |do while| loop is in the |ex_do_while_loop_SL_step| 
% function in |ex_do_while_loop_SL.c|:
file = fullfile('ex_do_while_loop_SL_ert_rtw','ex_do_while_loop_SL.c');
rtwdemodbtype(file,'/* Model step function */', ...
    '/* Model initialize function */',1,0);

%% Modeling Pattern for Do While Loop: Stateflow Chart
% 1. Open example model <matlab:ex_do_while_loop_SF |ex_do_while_loop_SF|>.
open_system('ex_do_while_loop_SF')

%%
% In the model, the |ex_do_while_loop_SF/Chart| executes the |do while| loop.
open_system('ex_do_while_loop_SF/Chart')

%%
% The chart contains a |While| loop decision pattern that you add by selecting
% *Chart* > *Add Pattern in Chart* > *Loop* > *While*.
%
% 2. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_do_while_loop_SF'')');

%%
% The code implementing the |do while| loop is in the |ex_do_while_loop_SF_step| 
% function in |ex_do_while_loop_SF.c|:
file = fullfile('ex_do_while_loop_SF_ert_rtw','ex_do_while_loop_SF.c');
rtwdemodbtype(file,'/* Model step function */', ...
    '/* Model initialize function */',1,0);




