Java for each Loop

The for-each loop, also known as the enhanced for loop, is a control flow statement that allows you to iterate over the elements of an array or a collection in Java. It’s a looping statement that allows you to execute a block of code repeatedly for each element in the array or collection.

The for-each loop is similar to the for loop, except that it does not use an index variable to iterate over the elements. Instead, it uses a variable of the same type as the elements of the array or collection, and assigns the value of each element to this variable in turn.

Java for each

The syntax of the for-each loop is straightforward and consists of a for keyword followed by a variable declaration and a colon, and an array or collection expression. The loop body is a block of code in curly braces.

Here is the general syntax of the for-each loop in Java:

for (type variable : array or collection) {
  // code block to be executed
}

Here is an example of how you can use the for-each loop to iterate over an array in Java:

int[] numbers = {1, 2, 3, 4, 5};

for (int number : numbers) {
  System.out.println(number);
}

In this example, the numbers array is initialized with the values 1, 2, 3, 4, and 5. The for-each loop iterates over the elements of the numbers array and prints each element to the console. The variable number is of type int and is declared and initialized to the value of each element in turn.

The output of this for-each loop will be the numbers 1 through 5, each on a separate line.

Java for each Examples

Iterating over an array:

int[] numbers = {1, 2, 3, 4, 5};

for (int number : numbers) {
  System.out.println(number);
}

In this example, the numbers array is initialized with the values 1, 2, 3, 4, and 5. The for-each loop iterates over the elements of the numbers array and prints each element to the console. The variable number is of type int and is declared and initialized to the value of each element in turn.

Iterating over a list:

List<String> fruits = Arrays.asList("apple", "banana", "orange");

for (String fruit : fruits) {
  System.out.println(fruit);
}

Iterating over a map:

Map<String, Integer> prices = new HashMap<>();
prices.put("apple", 100);
prices.put("banana", 50);
prices.put("orange", 70);

for (Map.Entry<String, Integer> entry : prices.entrySet()) {
  System.out.println(entry.getKey() + ": " + entry.getValue());
}

In this example, the prices map is initialized with three key-value.

Nested for-each loop

A nested for-each loop is a for-each loop that is contained within another for-each loop. It allows you to iterate over the elements of an array or collection within another loop.

The syntax of a nested for-each loop is similar to that of a regular for-each loop, except that the inner loop is contained within the body of the outer loop. The inner loop iterates over the elements of the inner array or collection, while the outer loop iterates over the elements of the outer array or collection.

Here is an example of a nested for-each loop in Java:

List<String> fruits = Arrays.asList("apple", "banana", "orange");
List<String> vegetables = Arrays.asList("carrot", "pepper", "cucumber");

for (String fruit : fruits) {
  for (String vegetable : vegetables) {
    System.out.println(fruit + " and " + vegetable);
  }
}

In this example, the fruits list is initialized with three strings: “apple”, “banana”, and “orange”. The vegetables list is initialized with three strings: “carrot”, “pepper”, and “cucumber”. The outer for-each loop iterates over the elements of the fruits list, while the inner for-each loop iterates over the elements of the vegetables list. The code block inside the inner loop consists of a single statement that prints the combination of a fruit and a vegetable to the console.

The output of this nested for-each loop will be all possible combinations of a fruit and a vegetable, as follows:

apple and carrot
apple and pepper
apple and cucumber
banana and carrot
banana and pepper
banana and cucumber
orange and carrot
orange and pepper
orange and cucumber

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *