FuzzDuck
FuzzDuck
For each loop
FuzzDuck
For each loop

For each loop

The for each loop takes a collection and runs through each element it contains. The collection could be an array or an arraylist, which holds different elements, all of which we wanted to configure. With the for each loop, we can easily run through each element by calling the loop.

The structure of the for each loop consist of two things:
1. The int number, identifying the current element of the collection.
2. The collection, which is run through.

Example - Array with integers
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Declares an array of integers containing 1,3,5 --- //
 int[] numberArray = new int[]{1,3,5};

 // --- Running through the numbers in array --- //
 foreach (int number in numberArray){
  Console.WriteLine("Retrieving the number: " + number + " from Array");
 }

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Declares an array of integers containing 1,3,5 --- //
 int[] numberArray = new int[]{1,3,5};

 // --- Running through the numbers in array --- //
 for (int number : numberArray){
  System.out.println("Retrieving the number: " + number + " from Array");
 }

}
// --- Declares an array of integers containing 1,3,5 --- //
$numberArray = array ( 1, 3, 5 );

// --- Running through the numbers in array --- //
foreach ( $numberArray as $number ) {
 echo "Retrieving the number: " . $number . " from Array";
}
Example - Array with strings
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Declares an array of strings containing "One", "Three", "Five" --- //
 String[] letterArray = new String[]{"One","Three","Five"};

 // --- Running through the letters in array --- //
 foreach (String number in letterArray){
  Console.WriteLine("Retrieving the letter: " + number + " from Array");
 }

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Declares an array of strings containing "One", "Three", "Five" --- //
 String[] letterArray = new String[]{"One","Three","Five"};

 // --- Running through the letters in array --- //
 for (String number : letterArray){
  System.out.println("Retrieving the letter: " + number + " from Array");
 }

}
// --- Declares an array of strings containing "One", "Three", "Five" --- //
$letterArray = array ( "One","Three","Five" );

// --- Running through the letters in array --- //
foreach ( $letterArray as $number ) {
 echo "Retrieving the number: " . $number . " from Array";
}
Example - Arraylist with integers
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Declares an arraylist --- //
 ArrayList numbers = new ArrayList();

 // --- Adding the numbers 1,2,3 to arraylist --- //
 numbers.Add(1);
 numbers.Add(2);
 numbers.Add(3);

 // --- Running through the numbers in arraylist --- //
 foreach (int number in numbers) {
  Console.WriteLine("Retrieving the number: " + number + " from Arraylist");
 }

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Declares an arraylist --- //
 ArrayList <Integer> numbers = new ArrayList ();

 // --- Adding the numbers 1,2,3 to arraylist --- //
 numbers.add(1);
 numbers.add(2);
 numbers.add(3);

 // --- Running through the numbers in arraylist --- //
 for (Integer number : numbers) {
  System.out.println("Retrieving the number: " + number + " from Arraylist");
 }

}
Example - Arraylist with strings
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Declares an arraylist of strings containing "One","Two","Three" --- //
 ArrayList letters = new ArrayList();

 letters.Add("One");
 letters.Add("Two");
 letters.Add("Three");

 // --- Running through the letters in arraylist --- //
 foreach (String letter in letters) {
  Console.WriteLine("Retrieving the letter: " + letter + " from Arraylist");
 }

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Declares an arraylist of strings containing "One","Two","Three" --- //
 ArrayList <String> letters = new ArrayList ();

 letters.add("One");
 letters.add("Two");
 letters.add("Three");

 // --- Running through the letters in arraylist --- //
 for (String letter : letters) {
  System.out.println("Retrieving the letter: " + letter + " from Arraylist");
 }

}