---
title: "Indexed For Loop — AP Comp Sci A Definition & Exam Guide"
description: "An indexed for loop iterates over arrays and ArrayLists using a counter variable. Learn why it's the safe choice for modifying lists on the AP CSA exam."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/indexed-for-loop"
type: "key-term"
subject: "AP Computer Science A"
---

# Indexed For Loop — AP Comp Sci A Definition & Exam Guide

## Definition

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.

## What It Is

An indexed for loop is the classic three-part [for loop](/ap-comp-sci-a/unit-2/for-loops/study-guide/DJuLxKz6SiSAX2cEVmCt "fv-autolink") you write as `for (int i = 0; i < arr.length; i++)`. The [index](/ap-comp-sci-a/key-terms/index "fv-autolink") 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](/ap-comp-sci-a/key-terms/enhanced-for-loop "fv-autolink") (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 It Matters

Indexed for loops show up in nearly every coding question on the AP CSA exam. They're introduced with [iteration](/ap-comp-sci-a/unit-2/while-loops/study-guide/7qGsGOh1UKALAWpJhZOi "fv-autolink"), then become the default [traversal](/ap-comp-sci-a/key-terms/traversal "fv-autolink") 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.

## Connections

### Index variable (Unit 4)

The index variable is the engine of the indexed [loop](/ap-comp-sci-a/key-terms/loop "fv-autolink"). 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)](/ap-comp-sci-a/key-terms/indexoutofboundsexception)

This is the [signature](/ap-comp-sci-a/key-terms/signature "fv-autolink") 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](/ap-comp-sci-a/key-terms/expression "fv-autolink") on the same loop skeleton.

### [Infinite Loop (Unit 4)](/ap-comp-sci-a/key-terms/infinite-loop)

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.

## On the AP 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 Takeaways

- 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.

## FAQs

### 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.

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/indexed-for-loop#resource","name":"Indexed For Loop — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/indexed-for-loop","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/indexed-for-loop#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T00:50:32.288Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/indexed-for-loop#term","name":"Indexed for loop","description":"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.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/indexed-for-loop","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is an indexed for loop in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What's the difference between an indexed for loop and an enhanced for loop?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can you modify an ArrayList inside an indexed for loop?","acceptedAnswer":{"@type":"Answer","text":"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)."}},{"@type":"Question","name":"Should the loop condition be i < arr.length or i <= arr.length?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Why does removing elements from an ArrayList in a for loop skip items?","acceptedAnswer":{"@type":"Answer","text":"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."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Indexed for loop"}]}]}
```
