Indexed for loop

An indexed for loop is a for loop that uses an index variable (usually i) to step through positions of an array or ArrayList one at a time, giving you direct control over where you are in the structure and the ability to modify or skip elements as you go.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is Indexed for loop?

An indexed for loop is the classic three-part for loop you write as for (int i = 0; i < arr.length; i++). The index variable i is just a counter, but it does double duty. It tracks how many times the loop has run, and it tells you exactly which position you're looking at. You use it to access elements with arr[i] for arrays or list.get(i) for ArrayLists.

The power of an indexed loop comes from that control. You can start anywhere, stop anywhere, count backward with i--, skip elements with i += 2, compare neighbors like arr[i] and arr[i + 1], and most importantly, you can change the data as you iterate using arr[i] = newValue or list.set(i, newValue). An enhanced for loop (for-each) can't do most of that. The tradeoff is responsibility. You own the bounds, so if your condition lets i reach arr.length, you get an IndexOutOfBoundsException, and if i never changes, you get an infinite loop.

Why Indexed for loop matters in AP Computer Science A

Indexed for loops show up in nearly every coding question on the AP CSA exam. They're introduced with iteration, then become the default traversal tool for arrays, ArrayLists, and 2D arrays, where nested indexed loops handle rows and columns. The exam repeatedly tests whether you can pick the right loop for the job. When a problem says "modify each element" or "remove elements that meet a condition," an indexed loop is usually the correct answer, because enhanced for loops can't reassign elements and throw a ConcurrentModificationException if you add or remove from an ArrayList mid-loop. Boundary reasoning is the other big skill. Knowing whether i < arr.length versus i <= arr.length is correct is the difference between a working traversal and a runtime crash.

How Indexed for loop connects across the course

Index variable (Unit 4)

The index variable is the engine of the indexed loop. It's initialized once, tested every pass, and updated every pass. Everything the loop does flows from how you set up those three pieces.

IndexOutOfBoundsException (Unit 6-7)

This is the signature failure of an indexed loop. Write i <= arr.length instead of i < arr.length and the last iteration asks for a position that doesn't exist. Off-by-one bugs are the most common loop error the exam tests.

Increment/decrement operator (Unit 4)

The update step (i++, i--, i += 2) decides the direction and stride of your traversal. Reversing an array or checking every other element is just a different update expression on the same loop skeleton.

Infinite Loop (Unit 4)

If the update step never moves the index toward making the condition false (forgetting i++, or incrementing when you meant to decrement), the loop runs forever. The exam loves asking you to trace loops where the index goes the wrong way.

Is Indexed for loop on the AP Computer Science A exam?

Multiple-choice questions hand you a loop and ask what it prints, how many times the body runs, or which implementation works. A classic stem gives you an ArrayList and asks which version of a doubling or removal task is "LEAST likely to cause problems." The answer is almost always the indexed loop with list.set(i, ...), because the enhanced for loop can't write back to the list. On the FRQs, you'll write indexed loops yourself any time you need to modify elements, compare adjacent elements, traverse part of a structure, or remove items from an ArrayList (where you also need to remember not to increment i after a removal, since elements shift left). Graders check your bounds, so practice saying out loud why your condition is i < list.size() and not <=.

Indexed for loop vs Enhanced for loop (for-each)

Both traverse a collection, but an enhanced for loop (for (int x : arr)) only gives you a copy of each value, with no position and no way to reassign elements. An indexed loop gives you the position itself, so you can modify elements, traverse backward, skip around, or compare neighbors. Quick rule for the exam. Reading only? Either works. Changing, removing, or needing the position? Indexed loop.

Key things to remember about Indexed for loop

  • An indexed for loop uses a counter variable to visit each position of an array or ArrayList, accessed with arr[i] or list.get(i).

  • Valid indexes run from 0 to length - 1, so the loop condition should be i < arr.length (or i < list.size()), and using <= causes an IndexOutOfBoundsException.

  • Unlike an enhanced for loop, an indexed loop lets you modify elements with arr[i] = value or list.set(i, value), making it the safe choice for changing data during iteration.

  • When removing elements from an ArrayList inside an indexed loop, don't increment i after a removal, because the remaining elements shift left into the spot you just checked.

  • Changing the update step changes the traversal, so i-- walks backward and i += 2 hits every other element, but an update that never ends the loop creates an infinite loop.

  • Nested indexed for loops are the standard way to traverse 2D arrays, with the outer index for rows and the inner index for columns.

Frequently asked questions about Indexed for loop

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

It's the standard three-part for loop, like for (int i = 0; i < arr.length; i++), that uses a counter variable to access each position of an array or ArrayList directly. The index gives you full control over which elements you visit and lets you modify them.

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

An indexed loop gives you the position, so you can reassign elements, traverse backward, or compare neighbors. An enhanced for loop only gives you each value in order, read-only. If a question asks you to change or remove elements, the indexed loop is the answer.

Can you modify an ArrayList inside an indexed for loop?

Yes, and that's exactly when you should use one. Use list.set(i, value) to replace elements safely. Doing the same modification inside an enhanced for loop either does nothing (reassigning the loop variable) or throws an exception (adding/removing).

Should the loop condition be i < arr.length or i <= arr.length?

Use i < arr.length. Array indexes run from 0 to length - 1, so an index equal to length is out of bounds and throws an IndexOutOfBoundsException at runtime. This off-by-one error is one of the most common mistakes the exam tests.

Why does removing elements from an ArrayList in a for loop skip items?

When you call list.remove(i), every later element shifts one position left, so the next element slides into index i. If you still increment i, you jump right over it. The fix is to only increment i when you don't remove, or traverse the list backward.