matlab do while,Do While Loop

C Construct

num_iter = 1;

do {

flag = func();

num_iter++;

}

while (flag && num_iter <= 100)

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 ex_do_while_loop_SL.

777a7f3ee3b3b791a9c6ca13dc4a9269.png

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 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.

The code implementing the do while loop is in the ex_do_while_loop_SL_step function in ex_do_while_loop_SL.c:

/* Model step function */

void ex_do_while_loop_SL_step(void)

{

int32_T s1_iter;

/* Outputs for Iterator SubSystem: '/While Iterator Subsystem' incorporates:

* WhileIterator: '/While Iterator'

*/

s1_iter = 1;

/* SystemReset for Atomic SubSystem: '/func' */

func_Reset();

/* End of SystemReset for SubSystem: '/func' */

/* End of Outputs for SubSystem: '/While Iterator Subsystem' */

do {

func();

s1_iter++;

} while (flag && (s1_iter <= 100));

}

Modeling Pattern for Do While Loop: Stateflow Chart

1. Open example model ex_do_while_loop_SF.

eb20ec9ef3c2e11fc5abb9913c4b305f.png

In the model, the ex_do_while_loop_SF/Chart executes the do while loop.

2f83849ac3f44094205e735d64082b71.png

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.

The code implementing the do while loop is in the ex_do_while_loop_SF_step function in ex_do_while_loop_SF.c:

/* Model step function */

void ex_do_while_loop_SF_step(void)

{

int32_T num_iter;

/* Chart: '/Chart' */

num_iter = 1;

do {

func();

num_iter++;

} while (flag && (num_iter <= 100));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值