Enhanced for loop in Java is better when scanning a complete array instead of using a for loop. Its syntax is:for (data_type variable: array_name)
Here, array_name is the name of an array.
Enhanced for loop Java program
public static void main(String[] args) {
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
for (int t: primes) {
System.out.println(t);
}
}
}
Download Enhanced for loop program.
Output of program:
Java enhanced for loop strings example
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", "Python", "Ruby"};
for (String sample: languages) {
System.out.println(sample);
}
}
}