Go Down

Topic: code question (Read 519 times) previous topic - next topic

dinky

hi! I would just like to ask what does that i inside the for()
do the statement that followed?
will that  repeat the code inside the braces 20 times?
thanks!!!


for (int i=0; i<20; i++)
{
digitalWrite(13, HIGH); // turns pin 13 on
delay(250); // pauses for 1/4 second
digitalWrite(13, LOW); // turns pin 13 off
delay(250); // pauses for 1/4 second
}

Catcher

Yes it will repeat the code inside 20 times before it leaves that block of code.
http://arduino.cc/en/Reference/For

Code: [Select]

void setup()
{
  Serial.begin(9600);
 
  Serial.println("Before");
  for(int i = 0; i < 20; i++)
  {
    Serial.println(i);
  }
  Serial.println("After");
}

void loop()
{
}

Goofballtech

although I am sure you could find this with some searching.

i is a variable set up to inside the loop (in this case an an int type) to keep track of how many times the for loop has been repeated thus far.

int i=0 //sets an initial value
i<20 // if this statement is true run the loop again
i++ // add one to i

and yes. it will run 20 times.

first time i will be 0
second time i will be 1
........
........
........
twentieth time i will be 19
then on the twenty-first time i will be 20 which makes i  < 20 false and the loop exits.

the end.


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy