The foreach loop in C++

Tushar Nikam
3 min readMay 13, 2020

Introduction

Foreach loop is a control flow statement for traversing items in a collection. Foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter, they essentially say “do this to everything in this set”, rather than “do this x times”.

The foreach loop in C++ or more specifically, range-based for loop was introduced with the C++11. This type of for loop structure eases the traversal over an iterable data set. It does this by eliminating the initialization process and traversing over each and every element rather than an iterator. So let us dig into the respective foreach loop structure.

Working of the foreach loop in C++

So basically a for-each loop iterates over the elements of array, vector, or any other data sets. It assigns the value of the current element to the variable iterator declared inside the loop. Let us take a closer look at the syntax:

for(type variable_name : array_name)
{
loop statements
...
}

As we can see:

  • During the loop initialization, the elemental variable declaration is the part where we need to declare the variable which will iterate over the array . Here, the type is the data type of the variable_name
  • array_name is the name of the respective data set over which the loop will iterate,
  • loop statements are the different operations which the user can choose to perform over the corresponding elements with the use of the iterating variable.

Note: It is suggested to keep the data type of the variable the same as that of the array or vector. If the data type is not the same, then the elements are going to be type-casted and then stored into the variable.

Examples of foreach loop

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

Output:

The elements are: 1 2 3 4 5

Let’s break down the code and look at it line-by-line:

  • An array arr[] is initialized with some values {1 , 2 , 3 , 4 , 5}
  • Inside the loop structure, ‘i’ is the variable that stores the value of the current array element
  • arr is the array name which also serves as the base address of the respective array
  • As we can see, printing ‘i’ for each iteration gives us the corresponding array elements in contrast to the array indices in case of normal for loop

So, if you’r thinking about that

Bonus Point: While declaring the variable ‘i‘ we could also use the auto datatype instead of int. This ensures that the type of the variable is deduced from the array type, and no data type conflicts occur.

For example:

Examples of foreach loop

#include<iostream>using namespace std;int main(){int array[]={1,4,7,4,8,4};cout<<"The elements are: ";for(auto var : array){cout<<var<<" ";}return 0;}

Output:

The elements are: 1 4 7 4 8 4

Advantages and Disadvantages of the foreach loop in C++

1. Advantages of foreach loop

  • It eliminates the possibility of errors and makes the code more readable.
  • Easy to implement
  • Does not require pre-initialization of the iterator

2. Disadvantages of foreach loop

  • Cannot directly access the corresponding element indices
  • Cannot traverse the elements in reverse order
  • It doesn’t allow the user to skip any element as it traverses over each one of them

Conclusion

The foreach loop in C++ has its own pros and cons. The code is easy to read but it restricts some of the actions that the normal for loop offers. Hence, it completely depends on the user what he/she wants the loop to perform and choose accordingly

References

Thanks for reading

--

--