foreach

MamtaWardhani's avatar
Published Mar 27, 2025
Contribute to Docs

The foreach loop in C++ iterates over the elements of a container (array, vector, etc.) by handling initialization, condition checking, and incrementing automatically. It processes each element in sequence, performing the same operation on every item in the collection. Introduced in C++11, it’s also known as a range-based for loop and provides a simpler syntax for iterating through collections compared to traditional for loops.

This loop structure is commonly used when there is a need to access every element in a container sequentially, such as when processing all items in an array, printing vector contents, or applying a transformation to each element in a container.

The foreach loop offers several key advantages over traditional loops:

  • Reduces boilerplate code by eliminating manual initialization, condition checking, and incrementing.
  • Makes code more readable and easier to understand at a glance.
  • Prevents common loop errors like off-by-one mistakes or boundary violations.
  • Works with any container that provides begin() and end() iterators.
  • Provides consistent syntax regardless of the container type being used.
  • Makes code maintenance easier as it focuses on the operation rather than loop mechanics.

Syntax

for (type variable_name : container_name) {
    // code block to be executed
}

Parameters:

  • for: The keyword that initiates the loop structure
  • type: The data type of the elements in the container
  • variable_name: The variable that will hold the current element value in each iteration
  • container_name: The array, vector, or other container to iterate through
  • { }: The code block containing statements to be executed for each element

Note: It is recommended to keep the data type of the variable the same as that of the container elements to avoid type casting.

Example 1: Iterating Through Arrays with foreach Loop

The foreach loop simplifies array traversal by eliminating the need for index management and boundary checking:

#include <iostream>
using namespace std;
int main() {
// Initialize an array of integers
int numbers[] = {10, 20, 30, 40, 50};
cout << "Array elements: ";
// Using foreach loop to iterate through the array
for (int num : numbers) {
cout << num << " ";
}
return 0;
}

This example results in the following output:

Array elements: 10 20 30 40 50

In this example, the foreach loop iterates through each element in the numbers array, assigning each value to the num variable in sequence. This eliminates the need to manually access elements using array indices, making the code more readable and less prone to index-related errors.

Example 2: Using auto Keyword with foreach Loop

The auto keyword can be used with the foreach loop to automatically determine the data type of elements, making the code more flexible when working with different container types:

#include <iostream>
#include <vector>
using namespace std;
int main() {
// Creating a vector of strings
vector<string> messages = {"Hello", "World", "foreach", "loop", "example"};
cout << "Vector contents: ";
// Using foreach loop with auto keyword
for (auto message : messages) {
cout << message << " ";
}
cout << endl;
return 0;
}

This example results in the following output:

Vector contents: Hello World foreach loop example

The auto keyword instructs the compiler to automatically deduce the type of message from the container elements, making the code easier to maintain, especially when working with complex container types or when element types might change during development.

Codebyte Example: Processing Map Elements with foreach Loop

The foreach loop can effectively iterate through key-value pairs in associative containers like maps:

Code
Output
Loading...

When iterating over a map with the foreach loop, each element is a std::pair object, with the key accessed via .first and the value through .second. This makes it straightforward to work with key-value data without explicitly declaring complex iterator types.

To explore more C++ concepts, check out Codecademy’s Learn C++ course.

All contributors

Contribute to Docs

Learn C++ on Codecademy