how to go back in previous iteration

40 vues (au cours des 30 derniers jours)
vipin pandey
vipin pandey le 7 Déc 2015
Commenté : vipin pandey le 8 Déc 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...

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Déc 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 commentaires
Walter Roberson
Walter Roberson le 7 Déc 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.
vipin pandey
vipin pandey le 8 Déc 2015
i got the answer thank you for your valuable suggestion

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by