Réponse acceptée

Cedric
Cedric le 23 Oct 2013
Modifié(e) : Cedric le 23 Oct 2013

0 votes

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 commentaires

trilochan
trilochan le 23 Oct 2013
can we use if condition inside the while loop
Cedric
Cedric le 23 Oct 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by