how to terminate nested loop
조회 수: 1 (최근 30일)
이전 댓글 표시
My Matlab code with nested loop is like the following:
for{
while{
if{
break
}
}
}
I want the if statement to stop the while loop and do the next for loop.
It's obvious break will terminate the for loop as well.
How can I change that?
댓글 수: 0
채택된 답변
Walter Roberson
2016년 4월 7일
"break" only acts on the immediately enclosing loop. It already does what you want.
For example,
>> for K = 1 : 5; X = 0; while true; X = X + 1; disp([K,X]); if X>K; break; end; end; end
1 1
1 2
2 1
2 2
2 3
3 1
3 2
3 3
3 4
4 1
4 2
4 3
4 4
4 5
5 1
5 2
5 3
5 4
5 5
5 6
You can see from the output that the break is working but that the containing "for" continues.
댓글 수: 0
추가 답변 (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!