Using an or in a while loop.

78 ビュー (過去 30 日間)
Matt Merkley
Matt Merkley 2018 年 1 月 23 日
編集済み: Stephen23 2018 年 1 月 23 日
I want to have an or statement in my while loop. Eventually I will be incrementing ea as well as iter. But right now it should exit out of the while loop after 100 iterations but it is continuing forever. What is wrong with the or statement? es = .01; imax = 100; iter = 0; ea = es;
while ((ea <= es )|| (iter <= imax) )
iter = iter + 1;
end
disp(iter);
  1 件のコメント
Stephen23
Stephen23 2018 年 1 月 23 日
編集済み: Stephen23 2018 年 1 月 23 日
ea = es
means that the first part of the condition ea <= es is always true, and because of the || (or) the while condition will therefore always be true. Is that what you want?

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 23 日
You need && for that rather than || . Your existing code does not exit the loop until both parts are false, which never happens since you do not change ea or es.

その他の回答 (1 件)

カテゴリ

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