A "for-each" loop is used exclusively to loop through elements in an array.
Syntax :
for (datatype variableName : arrayName) {
// code block to be executed
}
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
We explain Java For Each Loop tutorial with example and syntax. You can learn Java For Each Loop web online topic in JAVA easily. You can download and print Java For Each Loop tutorial and material in pdf format.
Comments