필터 지우기
필터 지우기

how to terminate nested loop

조회 수: 1 (최근 30일)
Yuang
Yuang 2016년 4월 7일
답변: Walter Roberson 2016년 4월 7일
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?

채택된 답변

Walter Roberson
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개)

카테고리

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