What is this function for?

I’ve been looking at some arduino code tutorials and I’ve noticed a function that keeps appearing.

for (int i = 0; i < 256; i++)

What is it for?, what does it do?, where can I use it?

I’m a newbie, sorry if this question is just stupid. :confused:

It is a for loop and has 3 parameters
In the example you quote i is initialised to zero then while i is smaller than 256 the code block for the for loop, which follows it, is executed then 1 is added to i

For instance

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 256; i++)
  {
    //for loop code block inside { and }
    Serial.println(i);
  }
}

void loop()
{
}

prints the numbers 1 to 255 then exits the for loop

A for loop is used to repeat a code block a fixed number of times

Thanks for the quick reply! :slight_smile:

In what depends the number of times the code block loops or repeats itself?

How fast does it go from 0 to 256 and in what depends it's speed?

The initial value of the control variable, the conditional test and the increment/decrement determine the number of iterations.

The loop goes as fast as the code in the body of the loop, plus a small overhead for the loop control itself.

As a general point when stuck on this sort of thing , googling “ Arduino if” ( or whatever) will lead to a description of the item and an example in the Arduino reference .