Enhanced For Loops
Objective #1: Use "enhanced" for loops with arrays.
enhanced for loop | traditional for loop |
int[] scores = {99, 75, 82}; int sum = 0; // running total for (int value : scores) { System.out.print(value + " "); sum += value; } System.out.println(sum); |
int[] scores = {99, 75, 82}; int sum = 0; // running total for (int i = 0; i < scores.length; i++) { System.out.print(scores[i] + " "); sum += scores[i]; } System.out.println(sum); |
Objective #2: Use nested "enhanced" for loops with two-dimensional arrays.