Once you master standard for and while loops, Java offers a powerful, streamlined alternative: the enhanced for-loop (often called the “for-each” loop).
In this interactive practice set, we cover Topic 6.3: Enhanced For-Loops. This loop structure is specifically designed to traverse arrays cleanly and safely without ever having to worry about an ArrayIndexOutOfBoundsException. However, the College Board loves to test students on its strict limitations.
Key Concepts to Remember Before You Start:
- Read-Only Nature: An enhanced for-loop makes a copy of each primitive element. If you change the loop variable, you do not change the actual array.
- No Index Access: You do not have access to an
icounter variable. If you need to know the specific index of an element, you must use a standardforloop. - No Partial Traversals: An enhanced for-loop always starts at index
0and visits every single element from left to right. You cannot step by twos or go backward. - Syntax: The format is always
for (DataType variableName : arrayName).
Test your understanding of these critical rules with the 10 practice questions below!
Topic 6.3 Practice
double values named prices?for (DataType variableName : arrayName). The data type of the loop variable must match the elements stored inside the array. Option B uses “in”, which is syntax from Python, not Java.for (int s : scores) {
s = s + 5;
}
System.out.println(scores[0]);
s) is just a copy of the array element. Modifying s changes the local variable, but it does absolutely nothing to the original scores array. The array remains {80, 90, 100}.int total = 0;
for (int n : numbers) {
total += n;
}
System.out.println(total);
total accumulator variable. 1 + 2 + 3 + 4 = 10.arr[i] = 0), which requires a standard for loop.for (String w : words) {
w = w.toUpperCase();
}
System.out.println(words[0]);
w to point to a new uppercase String object does not change the reference stored inside the original array. The array remains unchanged.i += 2) to jump directly to even indices.int c = 0;
for (int num : arr) {
if (num > 0) {
c++;
}
}
System.out.print(c);
length - 1) behind the scenes, you cannot accidentally code an “off-by-one” error. It guarantees you will not get an ArrayIndexOutOfBoundsException.String result = “”;
for (String letter : letters) {
result = letter + result;
}
System.out.println(result);
result = letter + result;. This means the new letter is prepended to the *front* of the string. Iteration 1: “A”. Iteration 2: “B” + “A” = “BA”. Iteration 3: “C” + “BA” = “CBA”.Unit 6.3 Practice Complete!
Why Can’t Enhanced For-Loops Modify Arrays?
The most common trap students fall into on the AP exam is trying to write code like for (int x : array) { x = 0; } to reset an array. Why doesn’t this work?
When an enhanced for-loop runs, Java creates a brand new, temporary variable in memory (in this case, x). It copies the value from the array into x. When you set x = 0, you are only changing that temporary container. The original array remains completely untouched. If you need to change data or access specific indexes, you must rely on a standard for loop.
Ready for the Next Step?
Now that you have mastered creating arrays and traversing them with both standard and enhanced loops, it is time to put those skills together to build the actual algorithms tested on the AP exam.
Continue your AP CSA review by moving on to Topic 6.4: Standard Array Algorithms Practice, where we cover shifting, reversing, finding minimums, and calculating averages.