MATLAB Answers

Creating an infinite loop until an event occurs?

291 views (last 30 days)
Mark
Mark on 26 Jun 2012
Edited: Walter Roberson on 26 Jul 2019
I am trying to make a loop until a certain event happens. In my first drafts I simply let it run for a large number of steps. However, now that I am adding a few extra steps in between, and want it to stop, it is no longer viable. Before I could run
N=100 for t=1:N (calculations) End
Which would do the loop 100 times. However, now I want it to continue forever until a certain event occurs. So now I am getting to:
N=10000 for t=1:N (calculations)
if (calculations) End % and finish here completely and do not engage in any more loops
Else (more calculations) End % and this should repeat back to the ‘for’ statement
Is there any way I can have this loop continuing, and block it in the middle after a certain event? Otherwise, is there another way of doing it?

  0 Comments

Sign in to comment.

Accepted Answer

Stephen
Stephen on 26 Jun 2012
go = true;
while go
step1
step2
step3
if step3==stopcriteria
go=false
end
end

  2 Comments

Walter Roberson
Walter Roberson on 26 Jun 2012
Or
while true
step1
step2
step3
if step3==stopcriteria; break; end
end
Mark
Mark on 26 Jun 2012
Sorry it took a while to answer. I was playing with the code but it finally worked, and I thank you very much for your precise answers!

Sign in to comment.

More Answers (1)

Erencem Özbey
Erencem Özbey on 12 Jul 2018
what is step1 step2 step 3

  1 Comment

Melissa
Melissa on 25 Jul 2019
That just represents various lines of code. like what you want to do while in the "while true" loop

Sign in to comment.

Sign in to answer this question.

Tags