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

5 views (last 30 days)
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!

Answers (1)

Star Strider
Star Strider on 28 Apr 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 Comments
John
John on 28 Apr 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 on 28 Apr 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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!