Return to the start of while or continue the script with an if condition

18 ビュー (過去 30 日間)
JESUS LOPEZ ARENAL
JESUS LOPEZ ARENAL 2022 年 6 月 6 日
編集済み: Geoff Hayes 2022 年 6 月 6 日
Hi, I have a while loop. Inside this loop I want to put an If condition, if true then return to the start of the while loop, if false then continue with the loop:
while i<j
a=rand
if a<0.5
i=i+1
disp('try again')
% and there I want to return to start of while loop
else
disp('continue')
% and there I want to continue with 'more code' section
end
% more code
end
Thanks in advance.

採用された回答

Geoff Hayes
Geoff Hayes 2022 年 6 月 6 日
編集済み: Geoff Hayes 2022 年 6 月 6 日
@JESUS LOPEZ ARENAL - you could put the more code into the else block, or just use continue as
while i<j
a=rand
if a<0.5
i=i+1
disp('try again')
continue;
end
disp('continue')
% more code
end

その他の回答 (0 件)

カテゴリ

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