Java For Each Loop

A "for-each" loop is used exclusively to loop through elements in an array.

Syntax :

for (datatype variableName : arrayName) {
  // code block to be executed
}

 

Example :

import java.util.*;

public class Main {
public static void main(String[] args) {

String[] fruits = {"Apple", "Banana", "Grapes", "Orange"};
for (String p : fruits) {
System.out.println("A Fruit is : "+p);
}
}
}

Output :

A Fruit is : Apple
A Fruit is : Banana
A Fruit is : Grapes
A Fruit is : Orange

Comments

Leave a Reply

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

95739