Repeat an iteration after verifying an if statement

7 ビュー (過去 30 日間)
Tejas Adsul
Tejas Adsul 2018 年 5 月 30 日
コメント済み: Tejas Adsul 2018 年 5 月 30 日
I have a while loop with 'i' going from 1 to 50. Inside the while loop, I have an if condition, which if it is true, should rerun that iteration. A simple example would be this:
i=1;
num = zeros(50,1);
while i<50
num(i) = randi(100);
if num(i)>70
%code to rerun this iteration
end
end
Suppose I get num(40) = 89. I want the iteration with i=40 to run again, so that num(40) gets a different value. I want the while loop to not increment 'i' if the 'if' condition is true. How should I do this? I tried i = i - 1, so that it would add 1 for the increment. But the increment doesn't happen, and i goes to negative values.
  1 件のコメント
Tejas Adsul
Tejas Adsul 2018 年 5 月 30 日
Oh! While loop doesn't automatically increment 'i'. We have to type it at the end of the iteration code. Got it! I can just skip the increment line if the condition is true.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by