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.
So basically a for-each loop iterates over the elements of arrays, vectors, 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/vector_name)
{
loop statements
...
}
As we can see:
type
is the data type of the variable_nameNote: 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.
The code given below illustrates the use of the for-each loop in C++,
#include<iostream>
using namespace std;
int main()
{
int arr[]={1,2,3,4,5}; //array initialization
cout<<"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:
arr[]
is initialized with some values {1 , 2 , 3 , 4 , 5}arr
is the array name which also serves as the base address of the respective arrayPlease note: 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:
#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 following code illustrates the use of the for-each loop for iterating over a vector
.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec={11,22,33,44,55,66};
cout<<"The elements are: ";
for(auto var : vec)
{
cout<<var<<" ";
}
return 0;
}
Output:
The for-each loop for vector works in the same way as it does for an array. Furthermore, the only differences are the vector declaration, initialization and the different operations that can be performed over it.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
can for each loop be used to to access data in Multidimentional arrays in c++??
- sonu