For each loop in C++

Hello Learners, today we are going to learn a very interesting topic that is foreach loop using C++ programming with implementation and working. Most of them will be familiar or heard of for loop, do, while, and do-while loop. For each loop is similar to it, but in for loop we don’t increment or give the decision parameters as for loop. In for each loop it directly copies the values of the array and prints, no need to initialize or give conditions inside the loop.

C++: Program for each loop

Foreach loop is also known as a range-based loop. It is a very easy method to copy the values from one variable to another variable, and it can print those values. Foreach loop eliminates the possibility of errors and makes the code more readable.

The main drawback of the for each loop is that it cannot traverse the elements in reverse order and cannot directly access the corresponding element indices.

First of all, looking into the code, let’s see the basic syntax of for: for each loop in C++ programming language

Syntax:

1)

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

2)

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

The basic difference between the first and second syntax is that in the second syntax no need of mentioning which datatype used it will take automatically. Now lets code for the for each loop, we will try to print the even and odd numbers from 1 to 10 which are already declared in an array with the help of for each loop.

Code:

#include <iostream>
using namespace std;
int main()
{
       int a[]={0,2,4,6,8,10,12,14,16,18,20};
       int c[]={1,3,5,7,9,11,13,15,17,19};
    cout<<"even numbers from 1 to 20"<<"\t";
    for(int b:a)
    {
        cout<<b<<"\t";
    }
    cout<<endl<<"odd numbers from 1 to 20"<<"\t";
    for(int d:c)
    {
        cout<<d<<"\t";
    }
    
    return 0;
}

Output:

even numbers from 1 to 20 0 2 4 6 8 10 12 14 16 18 20 
odd numbers from 1 to 20 1 3 5 7 9 11 13 15 17 19

Now let us modify this code and try to implement the same program using the second syntax: for each loop.

Code:

#include <iostream>
using namespace std;
int main()
{
       int a[]={0,2,4,6,8,10,12,14,16,18,20};
       int c[]={1,3,5,7,9,11,13,15,17,19};
    cout<<"even numbers from 1 to 20"<<"\t";
    for(auto b:a)
    {
        cout<<b<<"\t";
    }
    cout<<endl<<"odd numbers from 1 to 20"<<"\t";
    for(int d:c)
    {
        cout<<d<<"\t";
    }
    
    return 0;
}

Output:

even numbers from 1 to 20 0 2 4 6 8 10 12 14 16 18 20 
odd numbers from 1 to 20 1 3 5 7 9 11 13 15 17 19

If you are going to choose either of the syntax of foreach loop you are going to get the same output. I hope you understood this concept well, if you have any doubts please writes your queries in the comment session below.

Also, read: Get all the Files in a Directory using C++

One response to “For each loop in C++”

  1. Anirudh says:

    Thank you alot sir… Really appreciate it

Leave a Reply