필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
John
John 2014년 4월 28일
댓글: Star Strider 2014년 4월 28일
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!

답변 (1개)

Star Strider
Star Strider 2014년 4월 28일
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
John
John 2014년 4월 28일
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 2014년 4월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by