Using an or in a while loop.
57 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!