Effacer les filtres
Effacer les filtres

How to get back to the beginning of a loop when the loop ends

2 vues (au cours des 30 derniers jours)
John
John le 28 Avr 2014
Commenté : Star Strider le 28 Avr 2014
Okay my question might sound confusing, but here's what I'm trying to do.
I'm supposed to do some calculation within a loop, and then, at the end of that loop, I use whatever I get out to plug back into the beginning of the loop and basically start that same loop again. For example,
n = 1;
for n < 20
a = 20 + r
V = a + 4
if V < 20
some calculation
V = 4 * x + y
...
end
n = n + 1;
end
So, at the end, I get V equal to something.
Then I want to use this new value of V that I just got and put it back into the above loop to evaluate if this new V is less than 20. If it is, then perform everything in that loop again.
How do I do this?
Thank you so much!

Réponses (1)

Star Strider
Star Strider le 28 Avr 2014
Use a while loop:
r = 0.1; n = 1;
while n < 20
a = 20 + r
V = a + 4
if V < 20
some calculation
V = 4 * x + y
...
end
n = n + 1;
end
  2 commentaires
John
John le 28 Avr 2014
The 'r' in my question is just some input that the user has to enter.
So, how would you form that while loop exactly? I already have a while loop going on.
Thank you
Star Strider
Star Strider le 28 Avr 2014
Just the way I listed here. You can nest while and other types of loops.
I needed a value for r to test the code.

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