ESP32 / ESP8266 Arduino: Range-based for loop

In this tutorial we will learn how to iterate over an array of integers using a range-based for loop. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.

Introduction

In this tutorial we will learn how to iterate over an array of integers using a range-based for loop.

This tutorial was tested on both the ESP32 and the ESP8266, running the Arduino core. Nonetheless, this is a C++ feature, which means that it can be used outside the scope of microcontroller programming.

Range-based for loops were introduced in C++11 [1] and they are an alternative to the traditional for loops. They allow to iterate over an array with a more compact syntax, as we will see below.

The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.

The code

We will write our whole code in the Arduino setup function. The first thing we will do is opening a serial connection, to output the results of our program.

Serial.begin(115200);

Then we will define an array of integers, which will be the one we will iterate.

int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

After this we will write our range-based for loop. The syntax is the following [1]:

for ( range_declaration : range_expression ) loop_statement

The range_declaration corresponds to the declaration of a named variable that has the same type as the elements of the array [1]. In our case, it should be an integer, which we will call n. Thus, this should map to int n.

Note that in this case we are accessing the elements by value [1], which essentially means we are working with copies of the elements of the array. So, even if we change the value of the current iteration element inside our loop, the original elements of the array will remain unmodified.

However we can also declare a reference to the type of the elements of the array. For example, we could have used int & n instead as our range_declaration. In this case, since we would be working with references, we could change the values of the array by changing the current iteration element.

The range_expression corresponds to the sequence [1] that will be iterated. In our case, it will be our array arr.

Mapping this to our code, we get the following syntax:

for (int n : arr) {
    // loop implementation   
}

In our case, the implementation of the loop will simply consist on printing the current element of the array.

for (int n : arr) {
    Serial.println(n);    
}

The final source code can be seen below.

void setup() {

  Serial.begin(115200);

  int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  
  for (int n : arr) {
    Serial.println(n);    
  }


}

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device, using the Arduino IDE. When the procedure finishes, open the Arduino IDE serial monitor tool.

You should get an output like figure 1. As can be seen, we have obtained all the elements of the array, as expected.

Output of the program, showing all elements of the iterated array printed to the serial monitor.
Figure 1 – Output of the program, showing all elements of the iterated array. Taken from the tests on the ESP32.

References

[1] https://en.cppreference.com/w/cpp/language/range-for

2 Replies to “ESP32 / ESP8266 Arduino: Range-based for loop”

  1. Your statement:

    Then we will declare an array of integers, which will be the one we will iterate.

    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    Is a bit wrong. The statement above is a data definition, NOT a declaration. A data definition creates an attribute list for a data item. These attributes typically include data type (int), structure (array), scope (global) and perhaps other attributes of interest to the compiler. In this case, it includes an initializer list that allows it to fill in the element count for the array. Once the compiler is satisfied with the attribute list and its correctness (semantic and syntactic), it allocates memory for that data item. In this case it not only determines the memory address for the variable (i.e., its lvalue), it also fills in the array (its rvalues).

    A data declaration also constructs an attribute list, but does NOT allocate memory for it. For example:

    extern int arr[];

    is a data declaration, NOT a definition.

    Programmers are really sloppy about making the distinction, treating definition and declaration as being syntactically the same. They are not. As a retired university professor who taught languages for almost four decades, I know it is important for students to understand the difference from the outset.

Leave a Reply