(i'm new) Question about for statements (probably easy to most of you)

Hi I am new to arduino and I needed help on the "for" statements. on the arduino site, it shows: for(int x = 0; x < 100; x++)
could someone please explain this line step by step in newbie language :slight_smile: thx.
cmor

Hi
The first part initializes the variable i
The second part is the condition...when i reach a count of 100 then stop.
The third part increments the counter i

Hope this helps a bit
Rob

A for statement consists of three parts. The first part is the initialization section. In that example, the initialization section is "int x = 0". This declares x to be an int(eger) and assigns it an initial value of 0.

The second part is the continue condition. In the example, the continue condition is "x < 100". This means that the body of the for statement (the part between the { and } that follow) will be executed over and over, as long as x is less than 100.

The third part is the increment section. This code is executed after each execution of the body of the statement, before the continue condition is checked. In the example, the increment section is "x++". This means that x will be incremented by 1 after each execution of the body of the loop.

It then executes the one statement after the if successively until the terminating condition is met.
If you want more than one instruction to be executed then enclose them in curly braces { }
Some people recommend using them even if there is only one instruction.

how do i know what the variable # should be? thx again

rBroomfield:
Hi
The first part initializes the variable i
The second part is the condition...when i reach a count of 100 then stop.
The third part increments the counter i

Hope this helps a bit
Rob

Not really, since the example contained no variable named i.

Doh!!! sorry, i use 'i' i meant 'x'
I'll concentrate more.

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

Can be rewritten

{
  int x = 0;
  while (x < 100) {
    // body of the loop goes in here
    x += 1;
  }
}

in the arduino tutorial, it shows us how to create a for statement like this- for(int x = 0; x < 100; x++) but it does not say what each piece of it does. in the variable x = 0, what is the number zero (0) for and how could i find/make it?
How do I choose the x < 100 either? does it just stay at < 100, or do I edit it to match my own code? What is it?
and lastly, it says x++, what does this mean? what is the difference between x++ and x--? How do I choose which one to use in my code?
Thanks very much.
Cmor

for (int x = 0; sets the starting point. Can be any value you want.
x<100; sets the ending condition. Can be any test condition you want.
x++) sets how x changes. Can be any change you want.
In this case, x is incremented by 1 each pass thru the for:next loop: x++ is same as x=x+1.

Say you wanted the loop to go from 102 to 112 by twos:
for (int x = 102; x<113; x=x+2)

Say you wanted to count down from 100 to 1:
for (int x 100; x >0; x=x-1)

So how you go thru the loop is up to what you want your code to do.
You can even change the value of 'x' within the loop to end it sooner.

Google "C tutorial". That will explain a lot, including other similar questions you might have.

Re read reply #2 Paul told you exactly what all the parts do.