Azzera filtri
Azzera filtri

how to terminate nested loop

1 visualizzazione (ultimi 30 giorni)
Yuang
Yuang il 7 Apr 2016
Risposto: Walter Roberson il 7 Apr 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?

Risposta accettata

Walter Roberson
Walter Roberson il 7 Apr 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.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by