Effacer les filtres
Effacer les filtres

how to terminate nested loop

1 vue (au cours des 30 derniers jours)
Yuang
Yuang le 7 Avr 2016
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Avr 2016
"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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by