Update appNew update is available. Click here to update.
0% completed

Introduction

if statement

if-else statement

nested if statement

if-else-if statement

Find the output I

If statement I

Find the output II

Find the output III

If statement II

Find the output IV

Find the output V

Switch Statement

Find the output VI

Switch statement

Default case

Jump Statement

Goto statement

Skip iteration

0% completed

Find Character Case

Total Salary

Largest of three numbers

Find the Quadrant of the coordinate point

Loops

0% completed

Introduction

For Loop

While Loop

do-while Loop

for_each Loop

For loop

Loop execution

Goto label

Find the output VII

Find the output VIII

Find the output IX

0% completed

Sum of Even Numbers till N

Fahrenheit to Celcius

Sum of even & odd

Find the Power of a number

Find factorial of a number

Nth Fibonacci number

Basics Of C++
Report an issue
for_each Loop

iv)for_each loop: Apart from the standard loops such as “for, while and do-while,” C++  also allows using another functionality that solves the same purpose termed “for_each” loops. This loop accepts a function that executes over each of the container elements. 

This loop is defined in the header file “algorithm” and hence has to be included for this loop’s successful operation.

Advantages of for_each loop:

  • It is versatile, i.e., it can work with any container.
  • It reduces chances of errors one can commit using generic for loop
  • It makes code more readable
  • for_each loops improve the overall performance of code

Syntax:

 

 

for_each (InputIterator first, InputIterator last, Function fn)

 

Here,

  • first − Input iterator to the initial position.
  • last − Final iterator to the final position.
  • fn − Unary function that accepts an element in the range as an argument.

 

Example:

 

#include <iostream>
#include <algorithm>
using namespace std;
int print_even(int n) {
    if (n % 2 == 0)
        cout << n << ' ';
}
int main() {
    int arr[5] = {1, 2, 3, 4, 5};     
    cout << "The Array contains the following even numbers" << endl;
    for_each(arr, arr + 5, print_even);
    return 0;
}
Output:
The array contains the following even numbers
2 4

 

for_each loop can be executed using the keyword “for.”

 

Example: 

 

#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 2, 3, 4};
    // Printing elements of an array using foreach loop 
    for (int x: arr)
        cout << x << ' ';
    return 0;
}
Output:
1 2 3 4