Page 122 - AP Computer Science A, 7th edition
P. 122
(Read the top line as “For each element of type SomeType in collection ... ”)
Example
//Outputs all elements of arr, one per line. for (int element : arr)
System.out.println(element);
NOTE
1. The for-each loop cannot be used for replacing or removing
elements as you traverse.
2. The loop hides the index variable that is used with arrays.
THE while LOOP
The general form of the while loop is
while (booleantest) {
statements //loop body }
The boolean test is performed at the beginning of the loop. If true, the loop body is executed. Otherwise, control passes to the first statement following the loop. After execution of the loop body, the test is performed again. If true, the loop is executed again, and so on.
Example 1
int i = 1, mult3 = 3; while (mult3 < 20)
{
System.out.print(mult3 + " "); i++;
mult3 ∗= i;
} //outputs 3 6 18