For to While Loop

7 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2014 年 6 月 19 日
コメント済み: Giuseppe 2014 年 6 月 19 日
Hi I have been told you can convert any for loop to a while loop. If so I don't see how you could convert the following to a while loop?
for i = 1:5
disp(10 * i);
end
Thanks.

回答 (1 件)

Geoff Hayes
Geoff Hayes 2014 年 6 月 19 日
I'm not sure why a while loop would be better in this case (because it adds a couple of more lines of code) but here is the while equivalent to the above for loop
k = 1;
while k<=5
disp(10*k);
k=k+1;
end
Note that with this code, we have to manage the initialization of k, the incrementing of k and ensure that the condition has been set correctly.
  1 件のコメント
Giuseppe
Giuseppe 2014 年 6 月 19 日
Thanks I know it would not be better although I just wanted so see how the code could be written another way.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by