# What is the limit of the 'for' function?

**URL:** <https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922>\
**Category:** Programming\
**Created:** [February 8, 2023, 11:54pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922 "2023-02-08T23:54:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![saltyjoe](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@saltyjoe](https://forum.arduino.cc/u/saltyjoe)\
**Post date:** [February 8, 2023, 11:54pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/1 "2023-02-08T23:54:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [February 8, 2023, 11:58pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/2 "2023-02-08T23:58:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saltyjoe](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@saltyjoe](https://forum.arduino.cc/u/saltyjoe)\
**Post date:** [February 9, 2023, 12:00am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/3 "2023-02-09T00:00:34Z")

</div>

NICE!!! Thank you.

---

<div class="post-metadata">

**Author:** ![cedarlakeinstruments](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cedarlakeinstruments/32/1128931_2.png) [@cedarlakeinstruments](https://forum.arduino.cc/u/cedarlakeinstruments)\
**Post date:** [February 9, 2023, 12:00am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/4 "2023-02-09T00:00:49Z")

</div>

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

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

You'll probably be waiting a while...

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [February 9, 2023, 12:02am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/5 "2023-02-09T00:02:37Z")

</div>

You could get bad results using something like this:

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

```

Since `byte` goes from 0 to 255.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [February 9, 2023, 12:03am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/6 "2023-02-09T00:03:15Z")

</div>

> [@saltyjoe](#):
>
> 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 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.

---

<div class="post-metadata">

**Author:** ![saltyjoe](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@saltyjoe](https://forum.arduino.cc/u/saltyjoe)\
**Post date:** [February 9, 2023, 1:34am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/7 "2023-02-09T01:34:55Z")

</div>

```cpp

```

```cpp
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);
 }

 

 
}

```

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 9, 2023, 3:24am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/8 "2023-02-09T03:24:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [February 9, 2023, 3:29am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/9 "2023-02-09T03:29:06Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![tjaekel](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@tjaekel](https://forum.arduino.cc/u/tjaekel)\
**Post date:** [February 9, 2023, 4:42am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/10 "2023-02-09T04:42:30Z")

</div>

a for loop can be also "forever":

```cpp
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)

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [February 9, 2023, 5:40am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/11 "2023-02-09T05:40:32Z")

</div>

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...

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

```

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [February 9, 2023, 9:43am UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/12 "2023-02-09T09:43:52Z")

</div>

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

```

I think it should be:

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

```

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [February 9, 2023, 12:12pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/13 "2023-02-09T12:12:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saltyjoe](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@saltyjoe](https://forum.arduino.cc/u/saltyjoe)\
**Post date:** [February 9, 2023, 12:26pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/14 "2023-02-09T12:26:54Z")

</div>

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++?

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [February 9, 2023, 12:39pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/15 "2023-02-09T12:39:18Z")

</div>

Take a look in this code:

> [@Shorten Arduino code where Outputs are repeated to go High and LOW](https://forum.arduino.cc/t/shorten-arduino-code-where-outputs-are-repeated-to-go-high-and-low/1087332/14):
>
> could make press take an array and count.. const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A0, A1}; int relayStates[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; int delayTime = 100; void Press(int N[], int NumPins) { Serial.println("pressing pins"); for (int i=0;i\<NumPins;i++) { Serial.print(N[i]); digitalWrite(N[i] + 1, LOW); delay(delayTime); digitalWrite(N[i] + 1, HIGH); delay(delayTime); } } void setup() { Serial.begin…

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

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [February 9, 2023, 1:12pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/16 "2023-02-09T13:12:15Z")

</div>

Try this:

```cpp
#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()
{
}

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [February 9, 2023, 3:05pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/17 "2023-02-09T15:05:42Z")

</div>

> [@saltyjoe](#):
>
> Is there an easy explanation of the difference between ++x and x++?

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

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [February 9, 2023, 6:47pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/18 "2023-02-09T18:47:20Z")

</div>

> [@saltyjoe](#):
>
> Is there an easy explanation of the difference between ++x and x++?

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:

> <https://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c/9519095#9519095>

---

<div class="post-metadata">

**Author:** ![tjaekel](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@tjaekel](https://forum.arduino.cc/u/tjaekel)\
**Post date:** [February 9, 2023, 9:10pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/19 "2023-02-09T21:10:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [February 9, 2023, 10:30pm UTC](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922/20 "2023-02-09T22:30:33Z")

</div>

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

```cpp
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:

```cpp
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?

```

[Next page](https://forum.arduino.cc/t/what-is-the-limit-of-the-for-function/1087922.md?page=2)
