For-each loop in C++

For-each loop

On this page we will discuss for-each loop in C++.It is an auto increment loop and prints all the elements in the  container(arrays, vectors, list ques, etc).Ordinary loops must have the condition increments/decrements actions were as for each loop need not have any conditions and increments

for each loop
  • Setting up a for loop is long you need to take care of initialization, condition(s), increment/decrement.
  • In the case of arrays and vectors where the most frequent job is to print all items in them.
  • For each loop was introduced in C++ by developers.

Syntax

for (variable : collection)
{
// statement(s)
}

Example 1

Run
#include<iostream>
using namespace std;
int main()
{
    int arr[] = {10, 20, 30, 40, 50};
    // introduced in C++11
    // allows to print each item of array/vector
    for (int x : arr)
        cout << x << " ";
    return 0;
}

Output

10 20 30 40 50

Example 2

Run
#include<iostream>
using namespace std;
int main()
{
    char arr[] = {'P', 'a', 'r', 't', 'y'};
  
    // introduced in C++11
    // allows to print each item of array/vector
    for (char i : arr)
        cout << i << " ";
   return 0;
}

Output

P a r t y
  • This is very simple. Here, the variable m will go to every element of the array ar and will take its value.
  • So, in the first iteration, m is the 1st element of array ar i.earr[0]In the second iteration, it is the 2nd element i.e. arr[1 ]and so on. 

Limitation of for-each loop

  • It is applicable  only for arrays and container classes, it cannot be used for normal variables
  • It cannot be customized with conditions, I,e you can only print data from start to end

Why to use for_each

  • for_each loops improve the overall performance of code
  • It increases code readability

Example 3:

Run

#include<iostream>
using namespace std;
int main ()
{
  int n = 5;
  int items[n] = { 2, 4, 6, 8, 10 };
  cout << "Elements are: ";
  for (int i = 0; i < n; i++)
    {
      cout << " " << items[i];
    }
  cout << endl;
  int key = 6;
  cout << "element to be searched: ";
  cout << key;
  cout << endl;
for (int i:items)
    {
      if (i == key)
	{
	  cout << "element found" << endl;
	  return 0;
	}
    }
  cout << "element not found" << endl;
  return 0;
}

Output

Elements are: 2 4 6 8 10 
element to be searched: 6 
element found

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

One comment on “For-each loop in C++”


  • Kunal Kishor

    For each loop is basically used for accessing data from array, vector. .etc. It is designed to used when the Developer doesn’t know about the size of the data that are stored in Array . So, on that time for each loop will be used because under this loop we don’t have to describe the size . We just only declare a variable and all the accessed data are stored on this variable.