Control Flow
For Each in Java
The for-each loop iterates through every element in the array, without specifying size.
Syntax
//temporary iterator variable is declared in the loop
for(dataType iteratorVariable : IterableObject){
//the individual element is held in the iterator variable
//to access the value, just use iteratorVariable
}
Notes
Each element is assigned a temporary iterator variable, declared in the for-each structure.
An IterableObject is any object which implements the Iterable interface.
The iterator variable must be of the element's data type. Arrays are traversed in order (zero to size), but traversal order is not guaranteed for other Iterable objects.
Example
char[] studentGrades = {50, 89, 75};
for(char grade: studentGrades) {
System.out.println("Grade: " + grade);
} //prints each value of the array studentGrades (each loop is an element of studentGrades assigned to grade variable).