Indexed for loop in AP Computer Science A
In AP Computer Science A, an indexed for loop traverses an array or ArrayList with an index variable (usually i). The variable starts at a value, is checked against a condition like i < arr.length, and changes each iteration, so every element is accessed by its index, as in arr[i].
What is Indexed for loop?
An indexed for loop is a for loop that walks through a collection by position. You set up a counter (the index variable), give it a stopping condition, and tell it how to change each time through. The classic version looks like for (int i = 0; i < arr.length; i++). Inside the loop body, you reach each element with arr[i].
The CED says this directly. Per EK 4.4.A.2, traversing an array with an indexed for loop or while loop requires elements to be accessed using their indices. That separates it from the enhanced for loop, which hands you a copy of each element with no index at all (EK 4.4.A.3). Think of an indexed loop as walking down a row of numbered lockers. You always know which locker number you're at, so you can open it, change what's inside, peek at the next one, or skip every other one.
Why Indexed for loop matters in AP® Computer Science A
The indexed for loop lives in Unit 4: Data Collections, specifically Topic 4.4 Traversing Arrays. It supports learning objective AP Comp Sci A 4.4.A, which asks you to develop code that traverses the elements in a 1D array and determine the result of those traversals. EK 4.4.A.1 defines traversal as using repetition statements to access all or an ordered sequence of elements, and the indexed for loop is the most flexible way to do that.
It matters on the exam because it's the default tool for almost every array algorithm you'll write or trace. That includes sums, counts, finding a max or min, comparing neighbors, and changing values in place. If a problem needs the position of an element, or needs to modify the array, the indexed for loop is the one you'll reach for.
Keep studying AP® Computer Science A Unit 4
How Indexed for loop connects across the course
Enhanced For Loop (Unit 4)
Both loops traverse arrays in Topic 4.4, but the enhanced for loop gives you a copy of each element and no index (EK 4.4.A.3). Assigning a new value to that copy doesn't change the array (EK 4.4.A.4). So the rule of thumb is simple. If you need to know where you are or change what's stored, use the indexed version.
For Loop Structure and the Increment/Decrement Operator (Unit 2)
The indexed for loop is the same for loop you learned with iteration, just aimed at an array. The header's three parts (initialize, condition, update) usually use i++ or i--. Swap the update to i += 2 or start at arr.length - 1, and you can skip elements or traverse backward.
Traversing ArrayLists (Unit 4)
The same indexed pattern works on an ArrayList, with list.size() in place of arr.length and list.get(i) in place of arr[i]. Removing elements during a traversal shifts everything after that spot left. That's why index-based ArrayList loops sometimes run backward or skip incrementing after a removal.
2D Array Traversal (Unit 4)
A 2D array traversal is just one indexed for loop nested inside another. The outer index picks the row and the inner index picks the column. If you're comfortable with arr[i] in 1D, then grid[r][c] is the same idea with two counters.
Is Indexed for loop on the AP® Computer Science A exam?
On the multiple-choice section, the indexed for loop shows up mostly in code-tracing questions. You get a short segment and have to determine what's printed or what a variable holds afterward, which is exactly what LO 4.4.A asks. Typical stems:
- Conditional accumulation. Loop over
{3, 7, 2, 9, 1, 5}and addnumbers[i]only when it's odd (result: 25) or only when it's greater than 4 (result: 21). - Testing the index rather than the value.
if (i % 2 == 0)adds elements at even positions. For{10, 15, 20, 25, 30, 35, 40}that's 10 + 20 + 30 + 40 = 100. Watch whether the condition checksiornumbers[i], because that's a favorite trap. - Neighbor comparisons.
sum += data[i] * data[i + 1]with the boundi < data.length - 1. For{5, 2, 8, 1, 9}that's 10 + 16 + 8 + 9 = 43. The- 1keepsi + 1in bounds.
On free-response questions, you'll write indexed loops yourself to process arrays and ArrayLists. No released FRQ uses the phrase "indexed for loop" verbatim, but array and ArrayList questions regularly need one, especially when you must modify elements or work with positions. Expect to lose points for off-by-one errors such as i <= arr.length, which throws an ArrayIndexOutOfBoundsException.
Indexed for loop vs Enhanced For Loop
An indexed for loop (for (int i = 0; i < arr.length; i++)) gives you a position, and you access the element with arr[i]. An enhanced for loop (for (int x : arr)) gives you a copy of each element with no index (EK 4.4.A.3). The big consequence is that arr[i] = 0; actually changes the array, while x = 0; inside an enhanced loop changes only the copy (EK 4.4.A.4). Use indexed when you need positions, neighbors, partial ranges, or to modify values. Use enhanced when you just need to read every element in order.
Key things to remember about Indexed for loop
An indexed for loop traverses an array by using an index variable and accessing each element as arr[i].
The standard header is for (int i = 0; i < arr.length; i++), and using <= instead of < causes an ArrayIndexOutOfBoundsException.
Unlike an enhanced for loop, an indexed for loop can change the values stored in the array because it writes to arr[i] directly.
When the loop body uses arr[i + 1], the condition must stop at arr.length - 1 to stay in bounds.
In tracing questions, check whether a condition tests the index i or the value arr[i], because they give very different results.
The same indexed pattern carries over to ArrayLists (size() and get(i)) and to 2D arrays (nested loops with two indices).
Frequently asked questions about Indexed for loop
What is an indexed for loop in AP Computer Science A?
It's a for loop that uses a counter variable, usually i, to step through an array or ArrayList by position. Each element is accessed with its index, as in arr[i], which matches EK 4.4.A.2 in Topic 4.4 Traversing Arrays.
Can an enhanced for loop change the values in an array?
No, not for the values stored in the array. The enhanced for loop variable is a copy of each element, so assigning it a new value leaves the array unchanged (EK 4.4.A.4). To modify elements, use an indexed for loop and assign to arr[i].
What's the difference between an indexed for loop and an enhanced for loop?
An indexed for loop gives you each element's position (i), while an enhanced for loop gives you a copy of each element with no index. Indexed loops can skip elements, run backward, compare neighbors, and modify the array. Enhanced loops are simpler but read-only for the array's stored values.
Why do some loops use i < arr.length - 1 instead of i < arr.length?
The body accesses arr[i + 1], so the loop has to stop one early to avoid going past the last index. For example, with {5, 2, 8, 1, 9}, summing data[i] * data[i + 1] runs for i = 0 through 3 and gives 43.
Is the indexed for loop on the AP CSA exam?
Yes. It's central to learning objective AP Comp Sci A 4.4.A in Unit 4, where you trace and write array traversals. It shows up constantly in MCQ code tracing and is needed for most array and ArrayList FRQ code, even when the question never names it.
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.