 # For loop

I have read about the "For loop" statement but could someone explain why you would use such a statement. I have read the explanations on the web and in books and as I understand it counts through a loop.

Why would you use it. I have seen examples but dont understand "when" it would be used.

Well, generate the squares of the first ten integers. That would be one use.

Resetting/Initializing an array!! Putting 0x00 in every case for example.

to keep is very basic if I wanted 5apples. when I have 5 in a bag i then pay for them Is that kinda a "for loop" idea.

I don’t really think of it as one, no.

Imagine you have a bag of five apples.
You want to find out how many are red.

That might be a for loop sort of application.

thanks. what would tell me if it was red. which part… this is just hypothetical

for (int a=0; a <= 5; a++)

mark7w:
thanks. what would tell me if it was red. which part… this is just hypothetical

for (int a=0; a <= 5; a++)

The code that goes inside the loop. The for loop is just deciding how many times to execute whatever is in the block after it.

``````for(int a=0; a<5; a++)
{
checkAppleForRedness(apple[a]);   //Some function that checks one apple to see if it is red
}
``````

Assuming there is an array of apples in the bag. If not you could have two functions called takeOneAppleOut() and seeIfItIsRed().

The for loop determines how many times something happens, such as flashing an LED. The red apples examples is a bit contrived but something like this perhaps.

``````char * apples = {"red","green","green","red","green"};
int redCount = 0;

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

for (int a = 0; a < 5; a++)  //look at the colour of each of 5 apples
{                                    //statements inside this curly bracket pair will be executed 5 times
Serial.println(apples[a]);  //print the apple colour for apple number a
if (apples[a] == "red")      //test the colour of the current apple
{
redCount++;
}
}                                   //end of the for loop
Serial.print(redCount);
Serial.println(" red apples");
}
void loop()
{
}
``````

Thanks for taking the time to give an example. Mine was very contrived but I’m trying to get a better understanding of the for loop. Your example has helped me understand how it all comes together… Thanks all of you for the help.

The FOR loop is just shorthand:

``````for (a;b;c)
body;
``````

is just shorthand for:

``````a;
while (b) {
body;
c;
}
``````

`char * apples = {"red","green","green","red","green"};``apples[a] == "red"`
Not a great example.

It wasn't me that suggested counting red apples.

But at least using strcmp?

Well, I was trying to keep it simple and I managed to avoid using the 'p' word. Using the direct comparison it is very obvious what is going on, whereas strcmp introduces a level of complication that was not required in order to answer the original question about for loops.

Using strcmp I came up with`if (!strcmp(apples[a],"red"))`Is that better ? :)

I prefer apples at least it answered my question. ;)

Somethings almost always going to confuse someone …

``````enum { RED = 0, GREEN, YELLOW };

uint8_t apples = { RED, GREEN, GREEN, RED, YELLOW };

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

int match_counter   = 0;

for ( int i = 0; i < sizeof(apples); i++ )
{
Serial.println(apples[i]);
if (apples[i] == RED)
{
match_counter++;
}
}

Serial.print(match_counter);
Serial.println(" red apples");
}

void loop()
{   }
``````

johnwasser: is just shorthand for:

I've worked with 'C' compilers where that was true, but at some time in the distant past the scope of variables declared inside the for loop's control clause was redefined and they are no longer accessible outside the body of the for loop. Hence those two examples are no longer absolutely equivalent.

PeterH:

johnwasser: is just shorthand for:

I've worked with 'C' compilers where that was true, but at some time in the distant past the scope of variables declared inside the for loop's control clause was redefined and they are no longer accessible outside the body of the for loop. Hence those two examples are no longer absolutely equivalent.

Is this better?

``````{
a;
while (b) {
body;
c;
}
}
``````