How to break a for loop but run it one more time?
8 次查看(过去 30 天)
显示 更早的评论
I have a for loop that runs from i=1:n, but once an equation is satisfied in an if statement I want the for loop to end AND then run 1 more time, for example if it stops on 4 I want it to still run 5, then stop. Is there any way to do this?
0 个评论
回答(2 个)
Walter Roberson
2017-11-28
equation_is_satisfied = false;
for i = 1 : n
...
if equation_is_satisfied %it was satisfied on _previous_ loop iteration
break;
end
if some condition establishing that equation is satisfied on _this_ loop iteration
equation_is_satisfied = true;
end
end
0 个评论
另请参阅
类别
在 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!