フィルターのクリア

how to skip an iteration in a for-loop

244 ビュー (過去 30 日間)
Lou
Lou 2015 年 8 月 27 日
回答済み: Walter Roberson 2015 年 8 月 28 日
Hi, i have a for-loop and at the end of each iteration I want to check if a certain condition holds, if so, the loop is supposed to skip the next iteration. I tried it the (for me) obvious way:
for j=1:h
...do some interesting stuff...
if (z2>=f+tar(j+1)+1)
j=j+1;
end
end
it sets j to j+1 when it's supposed to but that doesn't effect the main for-loop at all. Where is my mistake? I could probably use a while-loop instead but it's supposed to be a for-loop... Thank you

採用された回答

the cyclist
the cyclist 2015 年 8 月 27 日
編集済み: the cyclist 2015 年 8 月 27 日
Use the continue command.
If you want to break out of the loop entirely (based upon some condition), use the break command.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 28 日
If you want to skip a future iteration then you will need to do something like
iterations_to_skip = 0;
for j=1:h
if iterations_to_skip > 0
iterations_to_skip = iterations_to_skip - 1;
continue;
end
....
if (z2>=f+tar(j+1)+1)
iterations_to_skip = 1;
end
...
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by