Skip to main content
United StatesComputer ScienceSyllabus dot point

When is the enhanced for loop the cleaner way to read every element of an array, and what can it not do?

Topic 6.3 Enhanced for Loop for Arrays: use an enhanced for-each loop to traverse the elements of an array, and explain why it cannot modify the array or access indices.

A focused answer to AP CSA Topic 6.3, covering the enhanced for-each loop syntax for arrays, how the loop variable holds a copy of each element, why it cannot change array elements or give the index, and when to use it versus a standard indexed loop, with a fully worked example.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. The enhanced for loop syntax
  3. It reads a copy, so it cannot modify the array
  4. No index available
  5. Try this

What this topic is asking

The College Board (Topic 6.3) wants you to use the enhanced for loop (the "for-each" loop) to traverse an array. It is a cleaner way to visit every element when you only need to read them and do not need their index. You must also understand its limitations: the loop variable holds a copy of each element, so assigning to it does not change the array, and the enhanced loop gives you no index.

The enhanced for loop syntax

int[] a = {10, 20, 30};
for (int item : a) {
  System.out.println(item);
}

Read the header as "for each item in a". On each pass, item is set to the next element: first 10, then 20, then 30. There is no index variable and no condition to write - the loop automatically visits every element exactly once, in order.

It reads a copy, so it cannot modify the array

int[] a = {1, 2, 3};
for (int x : a) {
  x = x * 10;   // changes the copy only
}
// a is still {1, 2, 3}

The assignment x = x * 10 updates the local variable x, but the array slots are untouched. This is the single most tested limitation of the enhanced loop.

No index available

The enhanced loop never exposes a position, so you cannot use it when the index matters - for example, to compare adjacent elements, to traverse part of the array, or to record where something was found. In those cases use the standard indexed loop.

Try this

Q1. After for (int x : a) { x = 0; } runs on int[] a = {5, 6, 7}, what does the array contain? [1 point]

  • Cue. Still {5, 6, 7}. The loop variable is a copy, so assigning to it does not change the array.

Q2. Give one situation where you must use a standard indexed loop instead of an enhanced for loop. [2 points]

  • Cue. When you need to modify the array's elements (assign through a[i]), or when you need the index, for example to traverse part of the array or to compare each element with its neighbor.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. Consider the following code segment. ```java int[] arr = {5, 1, 5, 3, 5}; int count = 0; for (int x : arr) { if (x == 5) { count++; } } System.out.println(count); ``` What is printed? (A) `3` (B) `5` (C) `2` (D) `0` (E) `15`
Show worked answer →

The answer is (A).

The enhanced for loop assigns x to each element of arr in turn: 5, 1, 5, 3, 5. The if (x == 5) increments count for each element equal to 5. The value 5 appears at three positions (indices 0, 2 and 4), so count becomes 3. (B) counts every element; (C) and (D) miscount; (E) sums rather than counts.

Markers reward recognizing that x takes each element value and counting how many equal 5.

AP 2021 (style)4 marksFree response (code writing). A `double` array `values` has been declared and initialised. Write a code segment that uses an enhanced for loop to compute and print the average of the elements. You may assume the array has at least one element.
Show worked answer →

A 4-point question testing an accumulation traversal with the enhanced for loop.

double sum = 0.0;
for (double v : values) {
  sum = sum + v;
}
System.out.println(sum / values.length);

Point 1: declare and initialise an accumulator sum to 0.0. Point 2: an enhanced for loop for (double v : values) whose loop variable type matches the elements. Point 3: add each element with sum = sum + v. Point 4: divide by values.length (a double divisor avoids integer truncation here since sum is already a double). The enhanced loop is ideal because we only read, never modify.

Related dot points

Sources & how we know this