This is an old revision of the document!


Table of Contents

Looping

For

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:

  • initialization section usually initializes the value of the variable that will be used to iterate the cycle; this value can be 0 or any other value;
  • condition allows to manage the number of cycle iterations; the statements in the body of the cycle are executed when the condition is TRUE;
  • operations with the cycle variable allows to define the number of cycle iterations.

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.

While

Do while

en/iot-open/programming_fundamentals_rtu/looping.1516782345.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0