Table of Contents

Loops

 General audience classification icon  General audience classification icon
Loops are critical to control flow structures in programming. They allow executing statements or some part of the program repeatedly to process elements of data tables and texts, making iterative calculations and data analysis. In the world of microcontrollers, where sometimes there is no operating system, the whole software works in the main loop called a super loop. It means the program never ends and works until the power is off.
This is clearly visible in the Arduino programming model, with one part of the code executed once after power-on setup(), and another executed repeatedly loop(). In C/C++, there are three loop statements shown in this chapter.

for

for is a loop statement that specifies the number of execution times of the statements inside it. Each time all statements in the loop's body are executed is called an iteration. In this way, the loop is one of the basic programming techniques used for all programs and automation in general.

The construction of a for loop is the following:

for (initialization ; condition ; operation with the cycle variable) {
  //The body of the loop
}

Three parts of the for construction are the following:

The example of the for loop:

for (int i = 0; i < 4; i = i + 1) 
{
    digitalWrite(13, HIGH); 		
    delay(1000);			
    digitalWrite(13, LOW);			
    delay(1000);	
}

On the initialisation of the for loop, the cycle variable i = 0 is defined. The condition states that the for loop will be executed while the variable i value will be less than 4 (i < 4). In operation with the cycle variable, it is increased by 1 each time the loop is repeated.

In the example above, the Arduino function digitalWrite is used. It sets the logical state high or low at the chosen pin. If an LED is connected to pin 13 of the Arduino board, it will turn on/off four times.

while

while loop statement is similar to the for statement but does not contain the cycle variable. Because of this, the while loop allows executing a previously unknown number of iterations. The loop management is realised using only condition that needs to be TRUE for the next cycle to execute.

The construction of the while loop is the following:

while (condition is TRUE)
{
  //The body of the loop
}

That way, the while loop can be used as a good instrument for the execution of a previously unpredictable program. For example, if it is necessary to wait until the signal from pin 2 reaches the defined voltage level = 100, the following code can be used:

int inputVariable = analogRead(2);
while (inputVariable < 100)
{
    digitalWrite(13, HIGH); 		
    delay(10);			
    digitalWrite(13, LOW);			
    delay(10);	
    inputVariable = analogRead(2);
}

In the loop above, the LED that is connected to pin 13 of the Arduino board will be turned on/off until the signal reaches the specified level.

do...while

The do…while loop works similarly to the while loop. The difference is that in the while loop, the condition is checked before entering the loop, but in the do…while, the condition is checked after the execution of the statements in the loop, and then if the condition is TRUE the loop repeats. As a result, the statements inside the loop will execute at least once, even if the test condition is FALSE.

The construction of a do while loop is the following:

do {
  //The body of the loop
} while (a condition that is TRUE);

If the same code is taken from the while loop example and used in the do…while loop, the difference is that the code will execute at least once, even if the inputVariable value is more than or equal to 100. The example code:

int inputVariable = analogRead(2);
do {
    digitalWrite(13, HIGH); 		
    delay(10);			
    digitalWrite(13, LOW);			
    delay(10);	
    inputVariable = analogRead(2);
} while (inputVariable < 100);