IF loop end if

FOR loop that runs i=1:N with an embedded IF loop that executes if val(i)>0 and val(i+1)<0 or opposite way around and outputs i. This works fine, but does take time.
How can I stop the FOR loop from continuing to run once it finds this value of i? What takes most running time is that it goes through the whole FOR loop range even if it finds the sign change early on.

Respuestas (1)

David Sanchez
David Sanchez el 26 de Jun. de 2013

0 votos

use break after you get the desired value. It will take you out of the for loop

3 comentarios

James
James el 26 de Jun. de 2013
Will that not just end the IF loop rather than the outer FOR loop?
David Sanchez
David Sanchez el 26 de Jun. de 2013
break only works with while and for loops, it has no effect on if conditions.
David Sanchez
David Sanchez el 26 de Jun. de 2013
break will terminate the closest for/_while_ loop.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Jun. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by