java / beginner
Snippet
The Enhanced For Loop
The enhanced for loop, or for-each loop, is a simplified way to iterate through every element in an array. It automatically moves to the next item until the end is reached.
snippet.java
1
2
3
4
String[] words = {"Java", "Code"};for (String w : words) {System.out.println(w);}
Breakdown
1
String[] words = {"Java", "Code"};
Creates an array containing two String elements.
2
for (String w : words) {
Starts a loop that assigns each element from the array to variable 'w' one by one.
3
System.out.println(w);
Prints the current value of 'w' to the console.