The for-each loop provides a mechanism for executing a block of code for each element in a collection. Some documentation (eg. Oracle's documentation) refers to these as enhanced for loops.
Here is the general syntax:
for(declaration: collection) {
body;
}
The declaration
declares the variable used to hold the values assigned from the collection.
The collection
is an array or collection holding the values that will be assigned to the loop variable.
The body
contains the statements that will be executed once for each value in the collection.
For example:
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for(char vowel: vowels) {
System.out.println(vowel);
}
which outputs:
a
e
i
o
u
Generally a for-each
loop is preferrable over a for
loop for the following reasons:
- A
for-each
loop is guaranteed to iterate over all values. - A
for-each
loop is more declarative meaning the code is communicating what it is doing, instead of how it is doing it. - A
for-each
loop is foolproof, whereas withfor
loops it is easy to have an off-by-one error (think of using<
versus<=
). - A
for-each
loop works on all collection types, including those that do not support using an index to access elements (eg. aSet
).