Can anyone replace FOR loop with WHILE loop?
Afficher commentaires plus anciens
for k=1:10
x=50-k^2;
if x<0
break
end
y=sqrt(x)
end
Réponses (1)
Andrei Bobrov
le 9 Déc 2013
Modifié(e) : Andrei Bobrov
le 9 Déc 2013
k = 0;
x = 0;
while x >= 0
k = k + 1;
x = 50 - k^2;
end
y = sqrt(x);
2 commentaires
misha Bilguun
le 9 Déc 2013
Walter Roberson
le 9 Déc 2013
Not in your code. Your "for" loop tests x<0 after it sets x to the new value. So the loop is not going to end until it is already true that x<0.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!