What is the limit of the 'for' function?

I want to repeat a part of my sketch 250 times. I can't tell how many times it gets repeated on the serial monitor. I see a whole bunch of what I put in Serial.print but it doesn't look like 250, but IDK, they blast across the screen. So, it works the way I hoped but I wonder if I'm asking too much from my UNO.

It's a simple sketch that is using 9% of program storage space and 14% of dynamic memory.

The limit is: unlimited.
You can place a counter together with messages to see how many you are getting.

NICE!!! Thank you.

unsigned long is 2^32 so that much if you are using the common

for (unsigned long int i = 0; i < ULONG_MAX; i++)
{
...
}

You'll probably be waiting a while...

You could get bad results using something like this:

for (byte i = 0; i < 300; i++)
{
  // do something
}

Since byte goes from 0 to 255.

So print incrementing numbers, then you can know.

I'm asking too much from my UNO.

No. You should post your sketch like the forum guidelines ask you to.



long secondsInMs(int s){ 
 return s*1000L;            //values will be expressed in seconds

}

long minutesInMs(int m){ 
 return m*60000L;           //values will be expressed in minutes 
}

 int Pump2 = 2;
 int Pump3 = 3;
 int Pump4 = 4;
 int Pump5 = 5;
 int Pump6 = 6;
 int Pump7 = 7;
 int Pump8 = 8;
 int Pump9 = 9;
 int Pump10 = 10;
 int Pump11 = 11;
 int Pump12 = 12;
 int Pump13 = 13;
 
 

void setup()

 

                     
{

Serial.begin(115200);
 
 
 pinMode(Pump2, OUTPUT);
 pinMode(Pump3, OUTPUT);
 pinMode(Pump4, OUTPUT);
 pinMode(Pump5, OUTPUT);
 pinMode(Pump6, OUTPUT);
 pinMode(Pump7, OUTPUT);
 pinMode(Pump8, OUTPUT);
 pinMode(Pump9, OUTPUT);
 pinMode(Pump10, OUTPUT);
 pinMode(Pump11, OUTPUT);
 pinMode(Pump12, OUTPUT);
 pinMode(Pump13, OUTPUT);
 
        
 

}

void loop() {

digitalWrite(Pump5, LOW);
digitalWrite(Pump6, LOW);
digitalWrite(Pump7, LOW);
digitalWrite(Pump8, LOW);
digitalWrite(Pump9, LOW);
digitalWrite(Pump10, LOW);

//Serial.print(" Circulation R off " );


digitalWrite(Pump2, HIGH);
digitalWrite(Pump3, HIGH);
digitalWrite(Pump4, HIGH);
digitalWrite(Pump11, HIGH);
digitalWrite(Pump12, HIGH);
digitalWrite(Pump13, HIGH);

Serial.print(" Circulation L on " );

delay(secondsInMs(4));

digitalWrite(Pump5, HIGH);
digitalWrite(Pump6, HIGH);
digitalWrite(Pump7, HIGH);
digitalWrite(Pump8, HIGH);
digitalWrite(Pump9, HIGH);
digitalWrite(Pump10, HIGH);
digitalWrite(Pump2, HIGH);
digitalWrite(Pump3, HIGH);
digitalWrite(Pump4, HIGH);
digitalWrite(Pump11, HIGH);
digitalWrite(Pump12, HIGH);
digitalWrite(Pump13, HIGH);

Serial.print(" All Pumps on  " );

delay(secondsInMs(4));


digitalWrite(Pump5,LOW);
digitalWrite(Pump6,LOW);
digitalWrite(Pump7,LOW);
digitalWrite(Pump11,LOW);
digitalWrite(Pump12,LOW);
digitalWrite(Pump13,LOW);

  for(int x = 0; x < 250; ++x)

 {

digitalWrite(Pump2, HIGH);
digitalWrite(Pump3, HIGH);
digitalWrite(Pump4, HIGH);
digitalWrite(Pump8, HIGH);
digitalWrite(Pump9, HIGH);
digitalWrite(Pump10, HIGH);

Serial.print(" Wave R on " );

delay(4);

digitalWrite(Pump2, LOW);
digitalWrite(Pump3, LOW);
digitalWrite(Pump4, LOW);
digitalWrite(Pump8, LOW);
digitalWrite(Pump9, LOW);
digitalWrite(Pump10, LOW);

Serial.print(" Wave R off  " );

delay(4);
 }

digitalWrite(Pump5, HIGH);
digitalWrite(Pump6, HIGH);
digitalWrite(Pump7, HIGH);
digitalWrite(Pump8, HIGH);
digitalWrite(Pump9, HIGH);
digitalWrite(Pump10, HIGH);

Serial.print(" Circulation R on " );


digitalWrite(Pump2, LOW);
digitalWrite(Pump3, LOW);
digitalWrite(Pump4, LOW);
digitalWrite(Pump11, LOW);
digitalWrite(Pump12, LOW);
digitalWrite(Pump13, LOW);

//Serial.print(" Circulation L off " );

delay(secondsInMs(4));

digitalWrite(Pump5, HIGH);
digitalWrite(Pump6, HIGH);
digitalWrite(Pump7, HIGH);
digitalWrite(Pump8, HIGH);
digitalWrite(Pump9, HIGH);
digitalWrite(Pump10, HIGH);
digitalWrite(Pump2, HIGH);
digitalWrite(Pump3, HIGH);
digitalWrite(Pump4, HIGH);
digitalWrite(Pump11, HIGH);
digitalWrite(Pump12, HIGH);
digitalWrite(Pump13, HIGH);

Serial.print(" All Pumps on " );

delay(secondsInMs(4));

digitalWrite(Pump2,LOW);
digitalWrite(Pump3,LOW);
digitalWrite(Pump4,LOW);
digitalWrite(Pump8,LOW);
digitalWrite(Pump9,LOW);
digitalWrite(Pump10,LOW);

 for(int x = 0; x < 250; ++x)

 {

digitalWrite(Pump5, HIGH);
digitalWrite(Pump6, HIGH);
digitalWrite(Pump7, HIGH);
digitalWrite(Pump11, HIGH);
digitalWrite(Pump12, HIGH);
digitalWrite(Pump13, HIGH);

Serial.print(" Wave L on " );

delay(4);

digitalWrite(Pump5, LOW);
digitalWrite(Pump6, LOW);
digitalWrite(Pump7, LOW);
digitalWrite(Pump11, LOW);
digitalWrite(Pump12, LOW);
digitalWrite(Pump13, LOW);

Serial.print(" Wave L off " );

delay(4);
 }

 

 
}

Actually the limit is infinite because the code can reset the value of "i" at any time and the "for" will never know about it.

Also, in C or C++ you are not limited to incrementing a variable. You could have:

   for (forLoopInit(); forLoopTest(); forLoopStep()) {
      // stuff
   }

and who would be able to tell WHAT would make it stop!

a for loop can be also "forever":

for (;;)

You will see this often when an RTOS is used (for an endlessly running thread).

I think, you are asking "how often do I have to iterate?".
But this depends on your "coding task":
Most of the time:

  • a for loop goes from start to end, where end is the MAX minus 1
  • or you can loop "forever" and when you see you reach the limit - you "break" the loop
    A for-loop is good for known sizes, e.g. dimension of an array, number of items in an array, "known" max. steps to do, e.g. to find something.

FOR is good for known length (number of iterations).
Otherwise you have also WHILE {} and DO {} WHILE: these are variable, they do not know a constant number how often to do, instead they break on a condition when to stop.

FOR is good for all which is countable, "predictable", known in size (number of iterations)...
Try to understand FOR, WHILE {} and DO {} WHILE and it becomes obvious when to use what.

How often a FOR loop can iterate?

  • for so long as your max. is reached on the variable type you use
    If it is byte, an int or even a long long - it just sets the max. number possible to encode.
    An unsigned byte just loops over 255, but if it is signed (as plus and minus) and you reach the max. of positive - just 127 times a loop positive (if you check for positive end values).

So, the type of the iteration variable matters for the max. potential number of iterations (but not for the reasonable number) - is it signed or unsigned?, is it byte?, int? or even long long? (a 64bit value, for instance). But you as programmer has to decide what is the "end of the iteration", the max. number you want to reach (or you should never exceed, e.g. outside of an array otherwise). The max. possible value via the variable types here does not matter so much.
Often we use "size_t" which is often the max. value for an unsigned int on platforms.

It matters more: "how many valid elements do you have to iterate over?" (e.g. an array size)

Are you trying to put your pumps on and off or are you trying to make a bit bang pwm?
4 ms is too short to start a pump.
For pwm I recommend non blocking code using timers (or builtin pwm, but you might run out of pwm pins).
Also, put your pumps in an array and use for loops to access them. It wiĺl save you approx 90% of your present code and will make changes far easier...

int nrOfPumps;
int pumpkins = {2,3,4,5,6};
for (int i=0; i<numberOfPumps; i++) {
   pinMode(i, OUTPUT);
   digitalWrite(i, HIGH);
}
for(int x = 0; x < 250; ++x)

I think it should be:

for(int x = 0; x < 250; x++)

++x and x++ are not the same, but give the same result here

Thanks guys, for the solid advice on writing tighter cleaner code. I appreciate that. I'll look up DO and DO WHILE. I was pretty happy to insert FOR and see it do what I hoped it would.
Is there an easy explanation of the difference between ++x and x++?

Take a look in this code:

I think that with some adjusts it can easily do what you are looking for.

Try this:

#include <Arduino.h>

#define NUM_RELAYS 12
const int relayPins[NUM_RELAYS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int relayStates[NUM_RELAYS] = {LOW};


void changeRelays(int N[], int NumPins, bool state)
{
  Serial.print("Changing pins ");

  for (int i = 0; i < NumPins; i++)
  {
    Serial.print(N[i]);
    digitalWrite(N[i], state);
    if(i < (NumPins - 2))
    {
      Serial.print(",");
    }
    else if(i < (NumPins - 1))
    {
      Serial.print(" and ");
    }
  }

  Serial.print(" to ");

  Serial.println(state ? "HIGH" : "LOW");
}

void setup()
{
  Serial.begin(115200);

  for (int i = 0; i < NUM_RELAYS; i++)
  {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], relayStates[i]);
  }

  const int pumpsPerSide = 6;
  int circulationR[pumpsPerSide] = {5, 6, 7, 8, 9, 10};
  int circulationL[pumpsPerSide] = {2, 3, 4, 11, 12, 13};

  changeRelays(circulationR, pumpsPerSide, HIGH);
  delay(1000);
  changeRelays(circulationR, pumpsPerSide, LOW);
  delay(1000);
  changeRelays(circulationL, pumpsPerSide, HIGH);
  delay(1000);
  changeRelays(circulationL, pumpsPerSide, LOW);
}

void loop()
{
}

Possibly, look for the difference by googling "pre and post increment in C"

The easy explanation is that with i++, you are telling the compiler to save a copy of the prior value to use in the next part of the line of code.

In the context of for(;;i++){} or even a plain i++; line, the compiler discards the copy as it moves on to the next line of code, and could possibly end up optimizing the value-saving step away.

This says there is a performance difference in favor of ++i; because the compiler isn't asked to store the prior value:

I think, ++x and x++ are the same in a for-loop. The x is used on the next iteration, not in the same statement.

Ahh, but is that the case if x is used within the loop, as an index?
Answer :grinning:

int val[] = {1,2,3,4,5,6};

void setup() {
Serial.begin(115200);
for(int x = 0; x < 4; x++) Serial.print(val[x]);
for(int y = 0; y < 4; ++y) Serial.print(val[y]);
Serial.println();
}

void loop() {
}

The output is?
And now, replace the two loops with:

for(int x = 0; x < 4; x) Serial.print(val[x++]);
for(int y = 0; y < 4; y) Serial.print(val[++y]);

What's the result now?