Skip to main content
United StatesComputer ScienceSyllabus dot point

How do you visit every element of an array in order using a standard for loop driven by the index?

Topic 6.2 Traversing Arrays: use a standard for loop with an index variable to traverse an array, reading or modifying each element, while staying inside the valid index bounds.

A focused answer to AP CSA Topic 6.2, covering how to traverse a one-dimensional array with a standard indexed for loop, the correct loop bounds, reading versus modifying elements, partial traversals, and avoiding off-by-one errors and out-of-bounds exceptions, with a fully worked example.

Generated by Claude Opus 4.811 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 standard traversal pattern
  3. Reading versus modifying
  4. Partial and reverse traversals
  5. Try this

What this topic is asking

The College Board (Topic 6.2) wants you to traverse a one-dimensional array: visit each element in order using a standard for loop whose index variable runs from 0 to length - 1. This is the workhorse pattern for arrays. Because the index drives the loop, you can both read each element and modify it in place, and you can traverse only part of an array when needed. The key discipline is keeping the index inside the valid bounds.

The standard traversal pattern

int[] a = {2, 4, 6, 8};
for (int i = 0; i < a.length; i++) {
  System.out.println(a[i]);
}

The loop control variable i is the index. It starts at 0, the condition i < a.length stops it after the last valid index, and i++ advances one element each pass. Inside the body, a[i] is the element at the current position.

Reading versus modifying

Because the index gives you direct access to each slot, a standard traversal can change the array, not just inspect it:

// read only: total the elements
int total = 0;
for (int i = 0; i < a.length; i++) {
  total = total + a[i];
}

// modify in place: double every element
for (int i = 0; i < a.length; i++) {
  a[i] = a[i] * 2;
}

This ability to write back through a[i] = ... is the main reason to choose a standard indexed loop over an enhanced for loop (Topic 6.3), which cannot change the array's elements.

Partial and reverse traversals

The index loop is flexible. You can traverse part of the array, or go backwards:

// first three elements only
for (int i = 0; i < 3; i++) { ... }

// from last element to first
for (int i = a.length - 1; i >= 0; i--) { ... }

The reverse loop starts at a.length - 1 and uses i >= 0 so it stops after index 0.

Try this

Q1. Why does a standard array traversal use the condition i < a.length rather than i <= a.length? [1 point]

  • Cue. The valid indices are 0 to a.length - 1; i < a.length stops exactly there, while i <= a.length would access index a.length and throw an ArrayIndexOutOfBoundsException.

Q2. Write a loop that adds 1 to every element of an int array a in place. [2 points]

  • Cue. for (int i = 0; i < a.length; i++) { a[i] = a[i] + 1; } - an indexed loop assigning back through a[i] so the array itself changes.

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 = {4, 8, 12, 16}; int sum = 0; for (int i = 0; i < arr.length; i++) { if (i % 2 == 0) { sum = sum + arr[i]; } } System.out.println(sum); ``` What is printed? (A) `16` (B) `24` (C) `40` (D) `12` (E) `20`
Show worked answer →

The answer is (A).

The loop visits indices 0, 1, 2, 3. The if (i % 2 == 0) adds an element only at even indices: index 0 (arr[0] = 4) and index 2 (arr[2] = 12). So sum = 4 + 12 = 16. (B) adds the odd-index elements (8 and 16); (C) adds all four; (D) takes only index 2; (E) mixes the wrong elements.

Markers reward tracing which indices satisfy the condition and summing exactly those elements.

AP 2021 (style)4 marksFree response (code writing). An `int` array `nums` has been declared and initialised. Write a code segment that uses a standard indexed for loop to replace every negative element of `nums` with 0, leaving non-negative elements unchanged. Modify the array in place.
Show worked answer →

A 4-point question testing in-place modification during an indexed traversal.

for (int i = 0; i < nums.length; i++) {
  if (nums[i] < 0) {
    nums[i] = 0;
  }
}

Point 1: a standard for loop with i from 0 to nums.length - 1 using i < nums.length. Point 2: test each element with nums[i] < 0. Point 3: assign nums[i] = 0 to modify the element in place. Point 4: leave non-negative elements unchanged (no else branch needed). Looping with i <= nums.length would throw an ArrayIndexOutOfBoundsException.

Related dot points

Sources & how we know this