admin 管理员组文章数量: 1086019
Flag = 0;
while Flag == 0
for x = 1:10
if x == 3
Flag = 1;
end % end: if
end % end; for
end % end: while
Can anyone tell me why the while loop condition is not triggering when x is three? It runs the entire for loop and I get a value of 10. I am trying to have a flag trigger when a specific condition is met in the for loop.
Thank you!
Flag = 0;
while Flag == 0
for x = 1:10
if x == 3
Flag = 1;
end % end: if
end % end; for
end % end: while
Can anyone tell me why the while loop condition is not triggering when x is three? It runs the entire for loop and I get a value of 10. I am trying to have a flag trigger when a specific condition is met in the for loop.
Thank you!
Share Improve this question asked Mar 27 at 18:30 Alexander SavadelisAlexander Savadelis 214 bronze badges 3 |1 Answer
Reset to default 4If you want to break out of the for
loop early then you should use break
Flag = 0;
while Flag == 0
for x = 1:10
if x == 3
Flag = 1;
break % Exit the 'for' loop
end
end
end
本文标签: For loop inside While loop not breakingMatlabStack Overflow
版权声明:本文标题:For loop inside While loop not breaking - Matlab - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744073402a2528903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
while
loop only checks its guard at the start of each iteration. – Scott Hunter Commented Mar 27 at 18:33break
orreturn
to exit thefor
loop early. – horchler Commented Mar 27 at 20:02