wait without delay() in a for-loop... How to do this?

wildbill: As discussed, you can manually adjust the iterator value in a for loop should you choose to, but when I see a for loop, I don't expect it. Such behaviour causes a bit of a double take: "What on earth is happening here?". I'll take PeterH's point that it's not banned by some higher power (Struggling with precise phrasing here), but to quote Jeff Goldblum's character in Jurrassic park: "Just because you could doesn't mean that you should".

My objection wasn't about manipulating a FOR loop's control variables within the body of the loop. I suppose there might be situations where that's a sensible thing to do, but in general it does not seem desirable because it's likely to encourage false assumptions about what the loop does.

On the whole I think I agree with PaulS' view of when a FOR loop is most suitable. I tend to think of it in terms of "for each thing in this set, do ...". I'll happily use it to do counted iteration, or consume the contents of a stream, or walk a memory structure, and so on. Other people may prefer to use different constructs in those situations, and that's fine. This is only a matter of personal preference though, and not a matter of right or wrong. The suggestion that it was [u]wrong[/u] to use a FOR loop for anything other than counted enumeration is what I objected to. It may not be your preference, but it isn't wrong.

I suppose there might be situations where that's a sensible thing to do, but in general it does not seem desirable because it's likely to encourage false assumptions about what the loop does.

Where I do loop variable manipulation inside a for loop is when I am altering the contents of an collection of values. In my job, I perform linear approximation of curves quite often. That process often produces more points than are really needed, so I delete co-linear points. I know how many points there are in the set, and how many times a loop must run. So, a for loop seems natural.

When I discover a co-linear point, by computing the angle between points n, n+1, and n+2, I'll remove point n+1 from the set.

The next pass through the loop would then deal with points n+1, n+2, and n+3. But, because there is the possibility of more than three points in a row being co-linear, I need to repeat the loop with the same index any time I remove a point from the set.

Since the loop increments the index before iterating, I will decrement the index inside the body of the for loop.

If the end condition of the loop is based on the number of points in the set, and that number of points is not determined each time it is needed, I'll manipulate the end condition variable inside the loop, too.

Other than cases like this, though, I agree that manipulating loop variables inside the loop is not a good idea.

WizenedEE:
This is much more readable:

for(i=0;i<30;)

{                   
     if (millis() - lastmillis < 25)    //  testing if the proper time has NOT passed
        {   
             otherRoutine();               // the other routine that needs to be regularly called
         }   
     else  {                                      // if the time HAS passed
       doSomething                          // do the occasional thing
       otherRoutine();                      // the other routine that needs to be regularly called
       lastmillis=millis();
       i++;
    }
   }




Or you could swap out the for loop for a while loop

for(i=0;i<30;) without an “incrementer” - who knew you could do that?!?

i do now.

Thanks everyone for a bunch of good and very valid answers. I now have some differing techniques to play with…
Especially the ‘while’ loop, as that seems more sensible for my application and also seems that it’d always be more readable than a tucked away i++; hidden somewhere unexpected…

I felt that for loop was there for “fixed” repetitions (but then why would break; exist?), at fixed increments and that is why I asked the Q: in the first place. It just “felt weird” to be tampering with the incrementing variable manually. I am glad my gut feeling was right. it means I AM learning. :wink:

I felt that for loop was there for "fixed" repetitions (but then why would break; exist?),

The break statement can be used in any loop - for, while, do/while, etc. It is most often used to break out of a while loop.

PaulS: The break statement can be used in any loop - for, while, do/while, etc. It is most often used to break out of a while loop.

Also "switch".