Tutorial Study Image

For each loop in C++


August 24, 2022, Learn eTutorial
1460

Along with more general looping methods like "for," "while," and "do-while," C++'s language also permits us to use "for-each" loops, which serve the same purpose. This loop accepts a function that runs through all of the container elements.

C++ 11 introduced the for-each loop,   and this for-loop structure makes traversing an iterable data set easier. It accomplishes this by omitting the initialization process and instead of using an iterator, traversing over each and every element. So let's take a look at the for-each loop structure.

In C++, how does the for-each loop work?

Syntax


for(type variable_name : array/vector_name)
{
    loop statements
    ...
}
 

A for-each loop basically iterates over the elements of arrays, vectors, or any other data sets. It assigns the current element's value to the variable iterator declared within the loop.

The elemental variable declaration is the part of the loop initialization where we declare the variable that will iterate over the array or vector. The type here is the data type of the variable_name.

The array/vector name is the name of the data set over which the loop will iterate.

loop statements are the various operations that the user can perform on the corresponding elements using the iterating variable.

Important Note: It is recommended that the variable's data type be the same as that of the array or vector. If the data types are not the same, the elements will be type-casted before being stored in the variable.

Examples of for-each loop

The code below demonstrates how to use the for-each loop in C++.


#include<iostream>
using namespace std; 
int main() 
{ 
    int num_array[]={1,2,3,4,5};   //array initialization
    cout<<"The elements are: ";
    for(int i : num_array)
    {
        cout<<i<<" ";
    }
    return 0;
}

Output:

The elements are: 1 2 3 4 5
Range Based for loop in C++

Let's take a look at the code line by line:

The array num_array[] is initialized with the values 1, 2, 3, 4, and 5.

Inside the loop structure, i is the variable that stores the current array element's value. num_array is the array name, which also serves as the array's base address.

As you can see, printing i for each iteration gives us the corresponding array elements instead of the array indices as in a typical for loop.

Please keep in mind that we could use the auto datatype instead of int when declaring the variable 'i.' This ensures that the variable's type is determined by the array type and that no data type conflicts occur.

For-each loop's benefits in C++

  • It eliminates the possibility of errors and improves the readability of the code.
  • very simple and easy to implement
  • Does not necessitate pre-initialization of the iterator 

The disadvantages of the for-each loop in C++

  • It is not possible to directly access the corresponding element indices.
  • It is not possible to traverse the elements in reverse order.
  • It does not allow the user to skip any of the elements as it goes through them all.

Conclusion

The for-each loop in C++ has advantages as well as disadvantages. The code is very simple to read, but it limits some of the actions that the standard for loop provides. As a result, the user must decide what he or she wants the loop to do and then choose accordingly.