programacion-bucle-foreach

The FOREACH loop

  • 4 min

The FOREACH loop is a control structure specifically designed to traverse and access the elements of a collection in a simplified manner.

For each element in this collection 🡆 do this

Which in code would look something like the following.

foreach element in collection
{
   // actions to perform on each element
}
Copied!

Where,

  • The collection is traversed from beginning to end
  • In each iteration element is updated with the current element
  • It continues until all elements have been traversed

programacion-bucle-foreach

FOREACH Diagram

In each iteration, the FOREACH loop assigns the next element of the collection to the variable element and executes the associated code block. This repeats until all elements in the collection have been processed.

The FOREACH loop simplifies performing loops over collections, resulting in more readable and concise code. Additionally, it avoids common errors related to incorrect element access.

Equivalence with Other Loops

If we had to replicate the same behavior with a WHILE loop, for example, we would have to do something like this

index = 0;
while(there_are_pending_elements)
{
	index ++;
	element = collection[index];
	
	// actions to perform with each element
}
Copied!

As we can see, this is the same behavior we defined earlier for the FOREACH loop. But it saves us the complexity of managing indices or iterators, allowing for more readable and concise code.

Examples of FOREACH Loops in Different Languages

The FOREACH loop is common in many programming languages. Some have a specific reserved word foreach, while others use the word for itself.

For example, in the case of C# the syntax of a FOREACH loop is like this,

var collection = new List<int> {1, 2, 3, 4, 5};

foreach (var element in collection) {
	Console.Write(element + " ");
}
Copied!

In the case of C++ (yes, it also has FOREACH!) it is similar to Java’s syntax, using the reserved word for.

auto collection = {1, 2, 3, 4, 5};

for (auto element : collection) {
	std::cout << element << " ";
}
Copied!

PHP also has a reserved word for FOREACH loops,

$collection = [1, 2, 3, 4, 5];

foreach ($collection as $element)
{
    echo $element . " ";
}
Copied!

Java, on the other hand, uses the reserved word for with a special syntax to perform a FOREACH loop, like this

var collection = new int[]{1, 2, 3, 4, 5};

for (var element : collection) {
	System.out.println(element);
}
Copied!

On the other hand, in Python the FOREACH loop is performed with the reserved word for. In fact, Python does not have a FOR loop as such, it is always a FOREACH loop.

collection = [1, 2, 3, 4, 5]

for element in collection:
    print(element)
Copied!

Finally, JavaScript does not have a FOREACH block as such, defined as part of the language. This was resolved with the .forEach(...) method, which is available as a method of the collection. So this is how we would achieve the functionality of a FOREACH loop.

let collection = [1, 2, 3, 4, 5];

collection.forEach(element => {
    console.log(element);
});
Copied!

And similarly in TypeScript

let collection: number[] = [1, 2, 3, 4, 5];

collection.forEach(element => 
{
    console.log(element);
});
Copied!

In the case of C++ (yes, it also has FOREACH!) it is similar to Java’s syntax, using the reserved word for.

auto collection = {1, 2, 3, 4, 5};

for (auto element : collection) {
	std::cout << element << " ";
}
Copied!

PHP also has a reserved word for FOREACH loops,

$collection = [1, 2, 3, 4, 5];

foreach ($collection as $element)
{
    echo $element . " ";
}
Copied!

Java, on the other hand, uses the reserved word for with a special syntax to perform a FOREACH loop, like this

var collection = new int[]{1, 2, 3, 4, 5};

for (var element : collection) {
	System.out.println(element);
}
Copied!

On the other hand, in Python the FOREACH loop is performed with the reserved word for. In fact, Python does not have a FOR loop as such, it is always a FOREACH loop.

collection = [1, 2, 3, 4, 5]

for element in collection:
    print(element)
Copied!

Finally, JavaScript does not have a FOREACH block as such, defined as part of the language. This was resolved with the .forEach(...) method, which is available as a method of the collection. So this is how we would achieve the functionality of a FOREACH loop.

let collection = [1, 2, 3, 4, 5];

collection.forEach(element => {
    console.log(element);
});
Copied!

And similarly in TypeScript

let collection: number[] = [1, 2, 3, 4, 5];

collection.forEach(element => 
{
    console.log(element);
});
Copied!

PHP also has a reserved word for FOREACH loops,

$collection = [1, 2, 3, 4, 5];

foreach ($collection as $element)
{
    echo $element . " ";
}
Copied!

Java, on the other hand, uses the reserved word for with a special syntax to perform a FOREACH loop, like this

var collection = new int[]{1, 2, 3, 4, 5};

for (var element : collection) {
	System.out.println(element);
}
Copied!

On the other hand, in Python the FOREACH loop is performed with the reserved word for. In fact, Python does not have a FOR loop as such, it is always a FOREACH loop.

collection = [1, 2, 3, 4, 5]

for element in collection:
    print(element)
Copied!

Finally, JavaScript does not have a FOREACH block as such, defined as part of the language. This was resolved with the .forEach(...) method, which is available as a method of the collection. So this is how we would achieve the functionality of a FOREACH loop.

let collection = [1, 2, 3, 4, 5];

collection.forEach(element => {
    console.log(element);
});
Copied!

And similarly in TypeScript

let collection: number[] = [1, 2, 3, 4, 5];

collection.forEach(element => 
{
    console.log(element);
});
Copied!

Java, on the other hand, uses the reserved word for with a special syntax to perform a FOREACH loop, like this

var collection = new int[]{1, 2, 3, 4, 5};

for (var element : collection) {
	System.out.println(element);
}
Copied!

On the other hand, in Python the FOREACH loop is performed with the reserved word for. In fact, Python does not have a FOR loop as such, it is always a FOREACH loop.

collection = [1, 2, 3, 4, 5]

for element in collection:
    print(element)
Copied!

Finally, JavaScript does not have a FOREACH block as such, defined as part of the language. This was resolved with the .forEach(...) method, which is available as a method of the collection. So this is how we would achieve the functionality of a FOREACH loop.

let collection = [1, 2, 3, 4, 5];

collection.forEach(element => {
    console.log(element);
});
Copied!

And similarly in TypeScript

let collection: number[] = [1, 2, 3, 4, 5];

collection.forEach(element => 
{
    console.log(element);
});
Copied!

On the other hand, in Python the FOREACH loop is performed with the reserved word for. In fact, Python does not have a FOR loop as such, it is always a FOREACH loop.

collection = [1, 2, 3, 4, 5]

for element in collection:
    print(element)
Copied!

Finally, JavaScript does not have a FOREACH block as such, defined as part of the language. This was resolved with the .forEach(...) method, which is available as a method of the collection. So this is how we would achieve the functionality of a FOREACH loop.

let collection = [1, 2, 3, 4, 5];

collection.forEach(element => {
    console.log(element);
});
Copied!

And similarly in TypeScript

let collection: number[] = [1, 2, 3, 4, 5];

collection.forEach(element => 
{
    console.log(element);
});
Copied!

Finally, JavaScript does not have a FOREACH block as such, defined as part of the language. This was resolved with the .forEach(...) method, which is available as a method of the collection. So this is how we would achieve the functionality of a FOREACH loop.

let collection = [1, 2, 3, 4, 5];

collection.forEach(element => {
    console.log(element);
});
Copied!

And similarly in TypeScript

let collection: number[] = [1, 2, 3, 4, 5];

collection.forEach(element => 
{
    console.log(element);
});
Copied!

And similarly in TypeScript

let collection: number[] = [1, 2, 3, 4, 5];

collection.forEach(element => 
{
    console.log(element);
});
Copied!

As we can see, most languages have the concept of FOREACH (understood as a loop that allows us to perform an action on all elements of a collection).

Some languages like C# or PHP have a reserved word foreach. While other languages like C++ or Java use syntax variations using for.

On the other hand, some languages like JavaScript, TypeScript, or Kotlin, lack a FOREACH loop native to the language. Instead, they provide the functionality through a method of the collection.

In any case, the functionality and objective are the same. Apart from syntax differences, they are basically equivalent.