can we use 'and' condition in "for"

 Respuesta aceptada

Cedric
Cedric el 23 de Oct. de 2013
Editada: Cedric el 23 de Oct. de 2013
You can't, it is always
for loopIndex = someArray
...
end
which is not condition-based. You can do it with WHILE statements though..
re1 = 0 ;
rh = 0 ;
while re1 < 0.5 && rh > -1
...
re1 = rand(1) ;
rh = randn(1) ;
end
You can also use break and continue in either loop statement, but we usually don't use them in the context that you describe. It is more for situations as follows
for k = 1 : maxRetry
...
found = ...
if found
break ;
end
end

2 comentarios

trilochan
trilochan el 23 de Oct. de 2013
can we use if condition inside the while loop
Cedric
Cedric el 23 de Oct. de 2013
You can use IF statements inside both loops, yes. As mentioned, the difference between these two loops is that one is based on a loop index which takes iteratively all values of a 1D array (or all columns of a 2D array), and the other on a loop condition. Other than that, you can do whatever you want inside both statements, and even nest them.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Preguntada:

el 23 de Oct. de 2013

Comentada:

el 23 de Oct. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by