I don't understand the "for" loop.

Hello.I want to make the code shorter and not so complicated with for loop.But i don't know how.I don't understand it.I watched some tutorials about it..Read some things about it on forums but nothing helps me.Can someone please explain it to me more detailed?I will be very grateful.Thank you:)

for (int counter = 0;counter < 10;counter++)
{
  Serial.println(counter);
}

The integer variable called counter will start at zero, will be checked to make sure that it is less than 10 and incremented each time through the loop. The commands in the curly brackets will be carried out each time through the loop. Once counter equals 10 the loop will stop and the program will carry on to the next line of code.

Does that help ?

just in case you haven't been here yet

http://arduino.cc/en/Reference/For

A for loop is similar to a while loop, except slightly different. Lets say you wanted to make a variable increase by one each time the loop runs, and stop when 4 is completed.

for(int x = 0; x < 5; x++) {
//Do something in here
//like print the current number
serial.println(x);
}

Lets run through it one part at a time.

int x

This creates a variable called x. Simple. X is the variable called in serial.println(x);.

x < 5;

This is sort of like an if statement. It says, “If x < 5, run x++ than do the stuff below.”
When x is equal to 5, and the for loop runs, this will cancel the operation and continue on past the loop.

x++

This is a very simple, but odd-looking , command. The ++ means, simply, ‘add one to x’. This part of the for statement runs when x < 5 only.

I hope this little walkthrough helped!

If you found this reply helpful, push the green + next to my name to give me karma (thanks).

Thank you guys. I will try and write some programs with it. BTW. I'm an amateur ,I just started with arduino and those things.I have no experience with C++ and programming, but I think I will figure it out.Thank you all.:)

t. It says, “If x < 5, run x++ than do the stuff below.”

Wrong. No karma for you, waterlubber.
The “x++” comes unconditionally at the END of the loop, then the test is made.

I will try and write some programs with it.

Trying something for yourself is important. Start with something simple like

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

  for (int counter = 0;counter < 50;counter++)
  {
    Serial.println(counter);
  }
}

void loop() 
{}

What is the output ?
Now change the for loop and observe the effects
What happens if you change it to

for (int counter = 10;counter < 50;counter++)

or

for (int counter = 0;counter < 50;counter = counter + 2)

or

for (int counter = 50;counter > 0;counter--)

Once you have got the hang of what is going on make some other changes and try to predict what will happen before you run it.
Can you write a for loop that counts down from 100 in steps of 5 and stops after it has printed 50 ?

The for loop is “syntactic sugar” - this means it is a convenient form used as a shorthand for several statements
combined together. The compiler will treat this for-loop:

for (int i = 0 ; i < 20 ; i++)
{
   // body
}

pretty much precisely as this code:

{
  int i = 0 ;
  while (i < 20)
  {
    // body
    i ++ ;
  }
}

Note the extra layer of {} which allow new variables to be declared in a scope specific to this for-loop only.

Any statements can go in the first and third slots in the for’s “parameters”, but the second must be an expression,
otherwise there are no rules, you could do this for instance:

for (alpha () ; beta () ; gamma ())
{
   // body
}

meaning:

{
  alpha () ;
  while (beta ())
  {
    // body
    gamma () ;
  }
}

The three “parameters” are all optional, the second one defaults to “true”, so that

for (;;)
{
  // body
}

Is just like

while (1)
{
  // body
}