MATLAB Answers

how to go back in previous iteration

94 views (last 30 days)
i was trying to run an iterative process in while loop.it is conditional based iteration, if the condition is satisfied in current iteration then continue to next iteration else go to previous iteration as long as condition is not satisfied.
i have considered iter=iter-1 for previous itertion in if loop and then used break command to terminate while loop . will it go back in previous iteration with this condition. suggestion will be appreciated.
thanks in advance...

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2015
The only way to go back to a previous iteration is to use a while loop and make the loop counter the appropriate previous value and then "continue".
It is much more common to want to stay on the current iteration until a condition is satisfied. For that, you can use a while loop within a for loop
for K = 1 : number_of_iterations
while true
do a calculation
if condition is satisfied
break
end %end if
end %end while
end %end for
  5 Comments
vipin pandey
vipin pandey on 8 Dec 2015
i got the answer thank you for your valuable suggestion

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by