Pages: [1]   Go Down
Author Topic: code question  (Read 463 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
}
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Safety first.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

void loop()
{
}
Logged

South Louisiana
Offline Offline
Full Member
***
Karma: 0
Posts: 180
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

MANY THANKS!!!!
Logged

Pages: [1]   Go Up
Jump to: