ESP32 / ESP8266 Arduino: Range-based for loop by reference

In this tutorial we will learn how to do a range-based for loop by reference, over an array if integers. 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 do a range-based for loop by reference, over an array if integers.

To illustrate that we are accessing each element of the array by reference, we will change the elements and print the array afterwards. For comparison, we will also test what happens if we access the elements by value (like covered here) and change them.

Note that the range-based for loops are a C++ feature, which means that the code shown here can be used outside the scope of microcontrollers programming.

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

Since for this tutorial we won’t need to include any library, we will start by the Arduino setup function. The first thing we will do, as usual, will be opening a serial connection, to be able to output the results of the program.

Serial.begin(115200);

Then we will define an array of integers, over which we will later operate.

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

The first thing we will do is a range-based for loop by value. Then, in each iteration, we will increment the current element by 10.

for (int n : arr) {
    n = n + 10;  
}

After finishing the previous range-based loop, we will print the value of all the elements of the array. The expectation is that they will keep their original values since were essentially working with copies of the elements of the array, in each iteration of the range-based loop.

for(int i = 0; i < 10; i++){
    Serial.print(arr[i]);
    Serial.print("|");
}

Now we will do a new range-based loop, this time by reference. We will again increment each element by 10.

for (int &n : arr) {
    n = n + 10;  
}

To finalize, we will again print the elements of the array. For this case, it is expected that they were changed, since we were working with references.

for(int i = 0; i < 10; i++){
    Serial.print(arr[i]);
    Serial.print("|");
}

The final source code can be seen below.

void setup() {
 
  Serial.begin(115200);
 
  int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
   
  for (int n : arr) {
    n = n + 10;  
  }

  Serial.println("Result of Iteration by value:");
  
  for(int i = 0; i < 10; i++){
    Serial.print(arr[i]);
    Serial.print("|");
  }

  for (int &n : arr) {
    n = n + 10;  
  }

  Serial.println("\nResult of Iteration by reference:");

  for(int i = 0; i < 10; i++){
    Serial.print(arr[i]);
    Serial.print("|");
  }
 
}
 
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device, using the Arduino IDE. After opening the serial monitor, you should obtain a result like the one on figure 1.

As can be seen, the first loop did not change the elements of the array, but the second did, as expected.

Output of the program, on the Arduino IDE serial monitor.
Figure 1 – Output of the program, comparing the results of both iterations. Taken from the tests on the ESP32.

Related Posts

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading