Arduino Code Help

Hello!

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

I know this little bit of code is probably very simple but for some reason I'm having a hard time understanding it.
It seems like 'i' would be reset to 0 ever time it was done with the statement in brackets.
How can you put multiple test statements inside of a if statement? When I try to add another one  like this
for(int i=0; i<10; i>3; ++i), it doesn't work. Why can't I add another one?
Why is it ++i? I thought it was supposed to be i++.

Could someone please explain it?

Thanks!


for(int i=0; i<5; ++i)
 {
   repeat something in here
   delay(100);
 }

sort by: active | newest | oldest
verence3 years ago

There are three statements inside the () after for. They might be empty, but itr are always three, never four or more. The are separated by ';'

The first one [int i=0] is executed once and only once when the loop logic starts.

The second statement [i<5] is logical expression. It is evaluated every time before entering the loop.

The third statement [++i] is executed every time after the loop is left - but before the second statement is evaluated.

int i=0 defines an Integer variable named i that is only valid inside the for statement. This is done once.

Than i is checked with i<5 (which is true here), so, the loops core (all the stuff between {}) gets executed.

The the third statement is executed. ++i increases i. i++ would do the same (if the compiler is not optimising, it is a tinsy bit less effective - but not enough for you to care about). i is now 1.

Next i is checked against i<5, still true {} gets executed, ++i ....

At last, ++i will increase i to 5. No i<5 is false and the loop is left.

You can use i inside the loop, you may even modify it (not best style as one does not expect it to get changed, but sometimes a nice hack.

You may omit some of the three statements:

int i=0;
for{;i<5;}
{
//do something
++i;
}

You can even define an endless loop

for{;;}
{
// use break to exit:
if (itGetsTooBoring)
break;
}

Some C dialects allow more than one part in the first and last statements:

for{int i=0, mask=1; i<5; ++i, mask*=2}
{ ... }

To get more than one relation in the exit expression, combine them into on logic expression:

for{int i=0; i<5 & !abortLoop; ++i}
{
....
if (letsAbortEarly)
abortLoop=true;
}
HavocRC (author)  verence3 years ago

Thank you very much verence! I understand that now!

The for statement runs until the i=4. Only then will it move on through the rest of the code. It should be i++ not ++i. If you want i to start at 3 then write the statement that way. for(i=3, i<10, 1++)

HavocRC (author)  mpilchfamily3 years ago

Yeah but how does it work? Why can't I add another statement with a semicolon after it? Like this?

for(int i=0; i<10; i>3; ++i)

I could have figured that out. What about my questions?

The the version of C the Arduino uses limits it to 3 statements inside the parenthesizes. So if you add more there the compiler doesn't know how to read it or handle it. The compiler is expecting the 3rd statement to increment the variable in some way. So you trip it up is you have a comparative statement in the 3rd slot.