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?

采纳的回答

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