Go Down

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


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?

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


Yes it will repeat the code inside 20 times before it leaves that block of code.

Code: [Select]

void setup()
  for(int i = 0; i < 20; i++)

void loop()


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