Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

Enhanced For Loop in AP Computer Science A

In AP Computer Science A, an enhanced for loop (for-each loop) traverses every element of an array or ArrayList in order. Each iteration assigns a copy of the current element to the loop variable, with no index involved, so you can read elements but can't use it to replace them or track positions.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is Enhanced For Loop?

An enhanced for loop is a shortcut for visiting every element in a collection, front to back. The header looks like for (int value : arr). Read it as "for each value in arr." Java handles the stepping for you, so there's no counter, no arr.length, and no arr[i].

The CED is precise about how the loop variable works. According to EK 4.4.A.3, on each iteration the enhanced for loop variable is assigned a copy of an element, without using its index. That one word, copy, explains most of the loop's behavior. EK 4.4.A.4 says that assigning a new value to the loop variable does not change the value stored in the array. Writing value = 0; inside the loop changes your copy and leaves the array alone. Think of it like reading names off a class roster. You can see every name, but scribbling on your notes doesn't edit the roster.

Why Enhanced For Loop matters in AP® Computer Science A

This term lives entirely in Unit 4: Data Collections, and it appears in three topics. In 4.4 Traversing Arrays you first meet it as an alternative to the indexed for loop. In 4.9 Traversing ArrayLists it comes with a warning label. In 4.12 Traversing 2D Arrays it gets nested. It supports three learning objectives: 4.4.A (traverse a 1D array and determine the results), 4.9.A (traverse an ArrayList and determine the results), and 4.12.A (traverse a 2D array and determine the results). The "determine the results" half matters most. The exam wants you to trace an enhanced for loop and predict exactly what it does, including when it fails to change data or throws an exception.

How Enhanced For Loop connects across the course

Indexed for loop (Unit 4)

These two loops are the two main ways to traverse a collection. Picking one comes down to a single question. Do you need to know where you are? If you need the position, a neighbor (arr[i+1]), a partial range, or the ability to replace elements, use an indexed loop. If you just need to look at every value once, the enhanced loop is cleaner and harder to mess up.

ArrayList traversal and ConcurrentModificationException (Unit 4)

EK 4.9.A.4 warns that changing an ArrayList's size during an enhanced for loop can throw a ConcurrentModificationException. The loop is quietly walking the list with an iterator, and calling add or remove pulls the floor out from under it. If you need to delete elements, switch to an indexed loop and handle the shifting yourself (EK 4.9.A.2).

2D arrays and Row-Major Order (Unit 4)

A 2D array is an array of arrays (EK 4.12.A.1), so a nested enhanced loop reads naturally as for (int[] row : grid) followed by for (int value : row). The outer variable is a whole row with type int[], and the inner variable is a single value. This always produces row-major order. Column-major order needs indexes, so the enhanced loop can't do it.

Object references (Unit 4)

The copy rule has a twist with objects. When the array holds objects, the loop variable gets a copy of the reference, so it points to the same object as the array does. Reassigning the variable still changes nothing, but calling a mutator method on it (like s.setScore(100)) does change the actual object in the array.

Is Enhanced For Loop on the AP® Computer Science A exam?

Enhanced for loops show up in tracing MCQs and in FRQ code you write.

Tracing questions. A classic stem nests two enhanced loops over a jagged 2D array like {{1, 2}, {3, 4, 5}, {6}} and asks what a running sum prints. The enhanced loop handles rows of different lengths automatically, so the answer is 21. Another common stem has the loop variable reassigned inside the body and asks what the array looks like afterward. The answer is "unchanged."

Limitation questions. Expect questions asking for the primary limitation of a nested enhanced loop, or which segment correctly finds the position of the maximum in a 2D array. Both test the same idea. An enhanced loop has no index, so it can't report a row and column without extra bookkeeping.

Exception questions. A method that calls scores.add(...) inside for (Integer score : scores) is a setup for a ConcurrentModificationException.

FRQs. The 2022 FRQ Q4 (a 2D array in a Data class) and the 2023 FRQ Q3 (temperature data in a WeatherData class) both reward traversal skill. Use an enhanced loop to read every value, and use an indexed loop when you need to modify the collection, remove from it, or track positions. Picking the wrong loop is a common way to lose points.

Enhanced For Loop vs Indexed for loop

Both loops visit every element, but they give you different things. An indexed for loop (for (int i = 0; i < arr.length; i++)) gives you the position i, so you can read arr[i], assign to arr[i], skip elements, go backward, or compare neighbors. An enhanced for loop gives you only a copy of each element, in order, from start to finish. So arr[i] = 0 zeroes the array, but value = 0 inside an enhanced loop does nothing to it.

Key things to remember about Enhanced For Loop

  • An enhanced for loop visits every element of an array or ArrayList in order without using an index.

  • The loop variable holds a copy of each element, so assigning a new value to it does not change the array or list.

  • If the elements are objects, the loop variable is a copy of the reference, so calling a mutator method on it does change the original object.

  • Adding or removing ArrayList elements inside an enhanced for loop can throw a ConcurrentModificationException, so use an indexed loop when you need to change the size.

  • For a 2D array, the outer enhanced loop variable is a whole row of type int[], the inner variable is a single element, and the traversal is always row-major.

  • If a task needs positions, neighbors, partial ranges, or replacement of elements, use an indexed for loop instead.

Frequently asked questions about Enhanced For Loop

What is an enhanced for loop in AP Computer Science A?

It's a loop like for (int x : arr) that visits every element of an array or ArrayList in order without an index. Each iteration assigns a copy of the current element to the loop variable (EK 4.4.A.3). It's covered in Topics 4.4, 4.9, and 4.12.

Can you change array values with an enhanced for loop?

No, not by assigning to the loop variable. Writing x = 5; only changes the copy, so the array stays the same (EK 4.4.A.4). The one exception involves arrays of objects, where calling a method like obj.setValue(5) changes the object the reference points to.

Can you remove elements from an ArrayList in an enhanced for loop?

No. Changing an ArrayList's size during an enhanced for loop can throw a ConcurrentModificationException (EK 4.9.A.4). Use an indexed for loop instead, and adjust the index (or loop backward) so that shifting elements don't get skipped.

What's the difference between an enhanced for loop and a regular for loop?

A regular (indexed) for loop gives you the position i, so you can read, replace, skip, or compare elements by index. An enhanced for loop gives you only each element's value, start to finish. Use enhanced when you just need to read every value, and use indexed when position matters.

How do you use an enhanced for loop on a 2D array?

Nest two loops: for (int[] row : grid) and then for (int val : row) inside it. The outer variable is an entire row (type int[]) because a 2D array is an array of arrays. This handles jagged arrays automatically and always traverses in row-major order.