how to go back in previous iteration
40 views (last 30 days)
Show older comments
vipin pandey
on 7 Dec 2015
Commented: vipin pandey
on 8 Dec 2015
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...
0 Comments
Accepted Answer
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
Walter Roberson
on 7 Dec 2015
If someone has imposed a constraint on you that a for loop must be used, then you will need to rethink your implementation.
Otherwise just convert the for loop to a while loop
i = 1;
while i <= 24
....
end
and remember to increment i when you are ready to move forward.
More Answers (0)
See Also
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!