Modifying counter within a loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Modifying counter within a loop

Fritz Sonnichsen
I have a stretch of code that contains a "for" loop. Within the loop,
under certain circumstances,  it adds a constant to the loop counter for
various reasons. This allows the code to "skip ahead". However after
some investigation it is apparent that one cannot do this--that is, the
loop counter is always set to the next consecutive value and ignores the
added constant.

I assume this is a rule for the loop command (could not find this in the
docs). Is there a way to override this behaviour?

Thanks
Fritz



Reply | Threaded
Open this post in threaded view
|

Re: Modifying counter within a loop

briankaz
I would use a while loop in this case and increment the counter in the while loop. Then you can be sure what value the counter has when it finishes one iteration of the loop and your counter skipping code should work.

-Brian 

On Thu, May 10, 2018, 20:07 Fritz Sonnichsen <[hidden email]> wrote:
I have a stretch of code that contains a "for" loop. Within the loop,
under certain circumstances,  it adds a constant to the loop counter for
various reasons. This allows the code to "skip ahead". However after
some investigation it is apparent that one cannot do this--that is, the
loop counter is always set to the next consecutive value and ignores the
added constant.

I assume this is a rule for the loop command (could not find this in the
docs). Is there a way to override this behaviour?

Thanks
Fritz





Reply | Threaded
Open this post in threaded view
|

Re: Modifying counter within a loop

Fritz Sonnichsen
Yep-that is the way to do it.
Funny thing-I haven't thought about this type of thing in 43 years and got caught by it!

thanks!
Fritz

On 5/10/2018 2:16 PM, Brian Kaczynski wrote:
I would use a while loop in this case and increment the counter in the while loop. Then you can be sure what value the counter has when it finishes one iteration of the loop and your counter skipping code should work.

-Brian 

On Thu, May 10, 2018, 20:07 Fritz Sonnichsen <[hidden email]> wrote:
I have a stretch of code that contains a "for" loop. Within the loop,
under certain circumstances,  it adds a constant to the loop counter for
various reasons. This allows the code to "skip ahead". However after
some investigation it is apparent that one cannot do this--that is, the
loop counter is always set to the next consecutive value and ignores the
added constant.

I assume this is a rule for the loop command (could not find this in the
docs). Is there a way to override this behaviour?

Thanks
Fritz