Using an or in a while loop.

조회 수: 74 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by