Effacer les filtres
Effacer les filtres

Invalid use of operator in for loop

14 vues (au cours des 30 derniers jours)
Anon
Anon le 22 Déc 2020
Commenté : Anon le 23 Déc 2020
Why am i getting an error message for this?
(am i also correct in using the '||' to mean 'or'?)
theta0 = tand(v0y/v0x)
for theta0 > 90 || theta0 < 0
% where the '>' gives an error message
end
if i switch the positions of the 'theta0 <0' and 'theta0 > 90' then i will get an error for the '<'
(whichever comes first has the error message)

Réponse acceptée

Joseph Wilson
Joseph Wilson le 22 Déc 2020
theta0 = tand(v0y/v0x)
while theta0 > 90 || theta0 < 0
% where the '>' gives an error message
end
Changing this to a while loop should fix your issue. A for loop counts for a specified number of iterations, the while loop iterates while some condition is met. So for your case you want this loop to run while theta0 is greater than 90 or less than 0. when that condition is no longer true, then the loop ends. Need to make sure that the code inside the loop changes theta0 or it will loop forever. If your do not plan on changing theta0 inside the loop, then Paul is correct above with using an if statement.
  1 commentaire
Anon
Anon le 23 Déc 2020
thank you very much!

Connectez-vous pour commenter.

Plus de réponses (1)

Paul
Paul le 22 Déc 2020
You're geting the error because that's the wrong syntax for a for loop:
doc for
Having said that, it kind of looks like you really want an if statement:
doc if
  1 commentaire
Anon
Anon le 23 Déc 2020
thank you paul! :3

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by