Enhanced for loop java

Submitted by:Tim Kelly

Date added:31 May, 2015

Category:Java

Enhanced for loop is useful when scanning the array instead of using for loop.

Tags: loop , integer array

Code Snippet:

// Java enhanced for loop integer array

class EnhancedForLoop {
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);
}
}
}



// Java enhanced for loop strings

class EnhancedForLoop {
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", "Python", "Ruby"};

for (String sample: languages) {
System.out.println(sample);
}
}
}
 
 

Comments