General For Loop Question

This will likely be a quick one; but I can’t seem to find a good answer online.

Pretty much every example of using a for loop statement is similar to this:

for (int thisPin = 2; thisPin < 8; thisPin++) {

Where I’m going to increment thisPin from 2 to 7.

Why does it seem that this is the general practice, versus using the actual range you intend to use:

for (int thisPin = 2; thisPin = 7; thisPin++) {

Wouldn’t this statement do the exact same thing?

No. Look at the "condition" clause of the second for loop. (Hint: 7 is non-zero. Non-zero values are true, even negative ones)

Three points here. The first is defensive programming - testing for an equality is more fragile than
testing for an inequality - comparing thisPin < 8 will work if thisPin happens to be incremented an
extra amount in the body of the loop.

Secondly = is not the equality test in C - classic error, be vigilant against this. ‘==’ != ‘=’

Thirdly this is because C uses zero-based indexing, so iterating an array of N elements looks like:

#define N <whatever>
int array[N];

for (int i = 0 ; i < N ; i++)
  do_something (array[i]) ;

The code handles N elements, they happen to be numbered 0 … N-1, but the code only has to use
the size of the array. This style of for loop is universally used in C, anything else will and does confuse
people used to this de-facto standard.

Quick question for you:

(Hint: 7 is non-zero. Non-zero values are true, even negative ones)

I don’t understand this, am I missing something?


I see my error on the ‘=’ vs ‘==’ and agree I should have written it that way. I also understand your statement of testing for inequality vs equality. That makes sense to me and at least for me would validate why you would want to use the ‘<’ vs the ‘==’

Let me then ask if this code looks okay like it is or should I consider doing something different.

My code is controlling LEDs arranged in a circle and face. So; if I want to change or update the eyes I update the LEDs with:

for (pixel = LeftEye[1]; pixel < LeftEye[1]+LenLeftEye+1; pixel++) {
	setPixel(pixel, R, G, B); 

Where LeftEye[1] is the starting LED and LenLeftEye is the number of leds in that eye. So that I include all the pixels I have to use the LeftEye[1]+LenLeftEye+1;

FYI: LeftEye is an array because I have 4 faces currently. LenLeftEye is a constant because all eyes contain the same # of LEDs.


You don’t use == because the condition in a for loop is a continue condition and not an end condition.

This for loop:

for (int i = 0; i == 7; i++){

Would not run one single time. 0 is not equal to 7 so it fails right out of the gate.

You can use

for (int i = 0; i <= 7; i++){

but then you have to remember to subtract one from the number of elements every time if it is an array. It’s just easier not to have to do that. For something like your pin number example I would probably use the <= so I could see the pin number I wanted to end on. It just depends on what you’re doing. Readability is all it affects.

If you want to count between 2 and 7 inclusive, then yes, a test for less-than-or-equal would be appropriate.

Most experienced programmers would put the pin numbers in an array and iterate over the array, in which case the strict less-than test would be more appropriate.

for (int i = 0; i == 7; i++){

When writing for loops, even simple ones, I recite what they mean to myself as a reminder

The above for loop won't work because it means set i to zero while i equals 7 increment i and perform the actions in the associated code block

The important part is that the second part of a for loop is actually implementing a while loop. In fact, a for loop can always be replaced by a while loop.