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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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.lengthstops exactly there, whilei <= a.lengthwould access indexa.lengthand throw anArrayIndexOutOfBoundsException.
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 througha[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
- Topic 6.1 Array Creation and Access: declare and initialise a one-dimensional array, access elements by index from 0 to length minus 1, and use the length attribute.
A focused answer to AP CSA Topic 6.1, covering how to declare and create a one-dimensional array, default element values, the length attribute, accessing and modifying elements by index, the valid index range, and ArrayIndexOutOfBoundsException, with a fully worked example.
- Topic 6.3 Enhanced for Loop for Arrays: use an enhanced for-each loop to traverse the elements of an array, and explain why it cannot modify the array or access indices.
A focused answer to AP CSA Topic 6.3, covering the enhanced for-each loop syntax for arrays, how the loop variable holds a copy of each element, why it cannot change array elements or give the index, and when to use it versus a standard indexed loop, with a fully worked example.
- Topic 6.4 Developing Algorithms Using Arrays: write and apply standard algorithms over a one-dimensional array, including finding minimum, maximum, sum, average, count and shifting or rearranging elements.
A focused answer to AP CSA Topic 6.4, covering the standard array algorithms - sum, average, minimum, maximum, count, search, and rearranging elements - the accumulator and running-best patterns, and how to combine them, with a fully worked example.
- Topic 4.2 for Loops: use a for loop, whose header combines initialisation, a boolean condition and an update, to repeat a block a controlled number of times, and convert between for and while loops.
A focused answer to AP CSA Topic 4.2, covering for-loop header syntax (init; condition; update), the order in which the three parts run, counting iterations, equivalence with while loops, common patterns, and tracing, with a fully worked example.
- Topic 4.5 Informal Code Analysis: determine the number of times a statement executes in a loop or nested loop by counting iterations, without using formal big-O notation.
A focused answer to AP CSA Topic 4.5, covering how to count statement executions in single and nested loops, the effect of step size and start/end bounds, conditional statements inside loops, and triangular versus rectangular counts, with a fully worked counting example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)