# Dynamic/Variable Dependent Infinite Loop?

So this is more of a “how to go about it” question rather than coding errors or anything.

I have a while loop that I need to run for as long as the user requests. Easy enough. What I can’t get my head around is how to allow the user to make this loop forever, or until the second condition of that while loop isn’t met anymore. If it’s easier to see what I’m talking about:

``````void blahBlah (unsigned long waitTime) {
if (waitTime == 0)  {
// Magic code to make the while loop forever
} else waitTime += millis();
while (!someThing || millis() <= waitTime) {
...
}
}
``````

The actual code is more involved obviously, and the loop takes a user defined amount of time to complete one iteration. I could do the math and add that time to waitTime inside the loop, but I’m just wondering if anyone has a more clever way to do this.

With a for loop this is straight forward:

``````int i = 0
if (whatever == 0 ) i = 1;
for (;i != whatever; i++) {
...
}
``````

Any help appreciated. Sorry if this has been answered before, I tried to google it but I couldn’t really think of a good wording and came up with nothing

``````while ( (waitTime == 0) || ! someThing || (millis() <= waitTime) ) {
``````

For the record, this is a bad idea…

``````(millis() <= waitTime)
``````

Apart from doing the maths on millis() the another way you may be able to use is the timer interrupts.
e.g. Arduino Playground - Timer Library

and the loop takes a user defined amount of time to complete one iteration. I

Well you are wrong there, because the while loop in your function there will iterate thousands of times, not one iteration, until your time millis() there reaches the specified value.

And in the arduino concept, the loop( ) function already repeats indefinitely. You are probably better off using this to keep doing what you are doing, rather than having the code lock up in a specific function.

If you want your while statement to run forever:

``````while ( true )
{
//  code here will be iterated over and over again,  for ever   (  not once )
}
``````

With a for loop this is straight forward:

Code:

``````int i = 0
``````

if (whatever == 0 ) i = 1;
for (;i != whatever; i++) {
...
}

This is not an infinite loop. i is an integer, and therefore when it gets to 32767, i++ will turn it into -32768, and then keep increasing it until it becomes 0, and therefore equal to whatever, and then stop the loop.

This is an infinite for loop (when whatever = 0):

``````for(; (whatever == 0) || (i != whatever); i++)
``````

haha Thanks. This is a cop-out but I'm new to coding

You think I should just compare times inside the loop, and break it there if needed?

@ roger

Wasn't aware of Timer.h, that will make other parts of this project simpler. Thanks man

@ michion

That must have been what Coding Badly meant. All of this was untested, just working out the logic. Thanks for pointing that out.

@ Trex

In my implementation I'd never hit that number but good to keep in mind. Wasn't aware you could use AND and all that inside the for loop brackets. Also good to know. Thanks