Leave the whole for-loop
Mostrar comentarios más antiguos
How can I leave the loop completely, if an "if" statement in that loop turns out to be true? I have tried break, but I guess that doesn't work in if-structures... My code structure looks like this:
for i = 1:5
(doing some stuff here)
if [something happens]
for [...], for [...], (assignments); end, end,
!and STOP COMPLETELY no further calculations are needed!
else
for [...], (assignments ); end
end
(doing more stuff)
end
Thanks for any help!
Respuesta aceptada
Más respuestas (2)
David Sanchez
el 30 de Ag. de 2013
The break command exists the most inner for/while loop. For nested for loops, you have to set a flag so that when it is active, the whole loop finishes. Following you code structure, break should exit:
for i = 1:5
(doing some stuff here)
if [something happens]
for [...], for [...], (assignments); end, end,
!and STOP COMPLETELY no further calculations are needed!
break % this break here will cause to get out of the main for-loop since it is not place within another for-loop
else
for [...], (assignments ); end
end
(doing more stuff)
end
Anna
el 30 de Ag. de 2013
0 votos
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!