This is an old revision of the document!
For is a cycle operator that allows to specify the number of times when the same statements will be executed. In this way, similar to the loop function it allows to control the program execution. Each time when all statements in the body of the cycle are executed, is called the iteration. In this way, the cycle is one of the basic programming techniques that is used for all programs and automation in general.
The construction of a for cycle is the following:
for (initialization ; condition ; operation with the cycle variable) { //The body of the cycle }
Three parts of the for construction is following:
The example of the for cycle:
for (int i = 0; i < 4; i = i + 1) { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); }
On the initialization of the for cycle the variable i=0 is defined. The condition states that the for cycle will be executed while the value of variable i will be less than 4 (i<4). In the operation with the cycle variable it is increased by 1 each time when the cycle is repeated.
In this example above, the LED that is connected to the pin 13 of the Arduino board will turn on/off four times.