Foreach Usage In PHP, JavaScript, Java, C#, Python Programming Languages with Examples – POFTUT

Foreach Usage In PHP, JavaScript, Java, C#, Python Programming Languages with Examples


Loops are an important part of the programming languages. For and While loops are used for decades in different programming languages. Foreach provides similar functionality in a more elegant way by iterating over the given list. In this tutorial, we will examine foreach in different programming languages like PHP, JavaScript, Java, C# and Python.

PHP Foreach

PHP provides foreach in order to iterate over given arrays. There is two main usage syntax about PHP foreach.

Value Syntax

In value syntax, we will provide just the array which is named ARRAY_EXPRESSION and the VALUE. We will write operations to the STATEMENT

foreach (ARRAY_EXPRESSION as $VALUE)
    STATEMENT

In this example, we will print array values to the standard output.

<?php
$arr = array(1, 2, 3, 4);

foreach ($arr as $value) {
    echo $value;
}
?>

Key, Value Syntax

In Key and Value syntax will also get the key from the array and use it in the foreach.

foreach (ARRAY_EXPRESSION as $KEY=>$VALUE)
    STATEMENT

In this example, we will print key and values into standard output

<?php
$arr = array(1, 2, 3, 4); 
foreach ($arr as $key=>$value) {
    echo $value;
}
?>

JavaScript Foreach

JavaScript is a very extensible language where most of the features are provided by objects. foreach is also provided by the object which is an array. It has the following syntax.

JavaScript Foreach Syntax

ARRAY.forEach(function(VALUE) {
  STATEMENT;
});

In this example, we will iterate over array named arr and print to the console.

var arr = ['a', 'b', 'c','d','e']

arr.forEach(function(element) {
  console.log(element);
});

Java Foreach

Java programming language actually does not provides native foreach keyword. But we can use other elements of Java like List, ArrayList and for.

For Syntax

We will use List and for to create foreach mechanism. TYPE is VALUE type and we will provide LIST.

for(TYPE VALUE:LIST){
  STATEMENT
}

We will iterate over string list and we will print to the standard output.

List<String> items = new ArrayList<>("A","B","C","D");

for(String item : items){
    System.out.println(item);
}

Foreach Syntax

After Java 8 List type provides forEach() function. So we can create a LIST and then iterate over elements with forEach() function. We will also use a lambda expression.

List<String> items = new ArrayList<>("A","B","C","D");
items.forEac(item->System.out.println(item));

C# Foreach

C # provides the foreach statement in a native way.  We will provide the LIST with the in keyword and set ELEMENT with the given TYPE.

foreach (TYPE ELEMENT in LIST){
  STATEMENT
}

In this example, we will iterate over an integer list named lst.

var lst= new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };

foreach (int element in lst) {

   Console.WriteLine($"Element {element}");

}

Python Foreach

Python programming language does not provide the foreach keyword but the actual implementation of the for in Python is hte same as the foreach. We can iterate over List, Array, Dictionary type in python with for like below.

for ELEMENT in LIST:
   STATEMENT

In this example, we will iterate over list named lst.

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

for element in lst:
   print element

LEARN MORE  Python While Loop Tutorial

Leave a Comment