AP CSP For Each Loops
AP CSP FOR EACH Loops & List Traversal: Complete Guide (2025‑2026)
FOR EACH item IN list iterates over every element in a list, setting the loop variable to each element in order. The number of iterations always equals the list length. The loop variable (item) is a copy of the element — modifying it does NOT change the list. FOR EACH is the primary tool for list traversal on the AP CSP exam and is heavily tested in combination with accumulators, conditionals, and the built-in list operations APPEND, INSERT, REMOVE, and LENGTH.
Contents
Structure and List Variable
The loop variable takes each value in order. Modifying item inside the loop does not change the original list element.
- Sets item = list[1] first iteration
- Sets item = list[2] second iteration
- Continues through the entire list
- Number of iterations = LENGTH(list)
- item is a copy — modifying it is safe
- Modifying item does NOT change the list
- FOR EACH does not provide an index variable
- FOR EACH cannot skip elements mid-traversal
- Appending to the list during iteration is risky
- FOR EACH cannot iterate backwards
FOR EACH with List Operations
Code Trace Gauntlet
What is total after the loop?
22 n=4: total=4. n=7: total=11. n=2: total=13. n=9: total=22. Display: 22.
How many scores are ≥ 90?
2 85≥90? F. 92≥90? T, count=1. 67≥90? F. 78≥90? F. 95≥90? T, count=2. Display: 2.
What is the maximum value found?
12 Starts at vals[1]=3. v=3: 3>3? F. v=8: 8>3? T, maxVal=8. v=1: 1>8? F. v=5: 5>8? F. v=12: 12>8? T, maxVal=12. v=4: 4>12? F. Display: 12.
What does doubled contain after the loop?
[2, 4, 6, 8, 10] Each element is doubled and appended to doubled. x=1: APPEND 2. x=2: APPEND 4. x=3: APPEND 6. x=4: APPEND 8. x=5: APPEND 10.
Spot the Bug
nums still contains [1, 2, 3] after the loop. Modifying n does not change the original list. Fix: use index-based access or build a new list with APPEND.
AP pseudocode lists are 1-indexed. list[0] is an error — the first element is list[1]. list[1] = ‘a’, list[2] = ‘b’, list[3] = ‘c’. Fix: DISPLAY(list[1]).
Common Exam Pitfalls
Unlike Python (0-indexed) and Java (0-indexed), AP pseudocode lists use 1-based indexing. list[1] is the first element. This is tested directly on the exam.
The loop variable is a copy of the element. Assigning a new value to it has no effect on the original list.
If the list has no elements, the loop body never executes. The accumulator retains its pre-loop value.
LENGTH([10, 20, 30]) = 3. It is NOT the last index (which is also 3 in 1-indexed) — it equals the count of elements.
Check for Understanding
1. data ← [2, 5, 1, 8, 3]
total ← 0
FOR EACH x IN data
total ← total + x
DISPLAY(total)
What is displayed?
- 19
- 5
- 15
- 8
2. items ← [10, 20, 30, 40]
DISPLAY(LENGTH(items))
What is displayed?
- 3
- 4
- 40
- 10
3. A list has 6 elements. How many times does the body of FOR EACH item IN myList execute?
- 5
- 6
- 7
- Depends on the body content
4. nums ← [3, 7, 2, 9, 1]
count ← 0
FOR EACH n IN nums
IF n > 4
count ← count + 1
DISPLAY(count)
What is displayed?
- 2
- 3
- 9
- 5
5. In AP pseudocode, what is the index of the first element of a list?
- 0
- 1
- Depends on how the list was created
- It has no index — FOR EACH is the only way to access elements
6. Consider statements about FOR EACH:
I. FOR EACH iterates over every element in a list in order.
II. Modifying the loop variable changes the corresponding list element.
III. FOR EACH on an empty list runs the body zero times.
Which are correct?
- I only
- I and III only
- II and III only
- I, II, and III
7. words ← ["cat", "dog", "bird"]
result ← ""
FOR EACH w IN words
result ← result + w
DISPLAY(result)
What is displayed?
- cat dog bird
- catdogbird
- bird
- cat
8. A student wants to find the minimum value in a list. Their initialization line is minVal ← 0. What is wrong?
- Nothing — 0 is always less than any value in the list.
- If all list values are positive, 0 will never be replaced and minVal will incorrectly stay at 0.
- The variable should be named minimum, not minVal.
- The loop variable should also be initialized to 0.
9. myList ← [1, 2, 3]
FOR EACH item IN myList
item ← item * 10
DISPLAY(myList)
What is displayed?
- [10, 20, 30]
- [1, 2, 3]
- [10, 2, 3]
- An error occurs
10. vals ← [5, 3, 8, 1, 9, 2]
newList ← []
FOR EACH v IN vals
IF v > 4
APPEND(newList, v)
DISPLAY(newList)
What is displayed?
- [5, 8, 9]
- [3, 1, 2]
- [5, 3, 8, 1, 9, 2]
- [8, 9]
How the AP Exam Tests This
- Trace a FOR EACH loop with an accumulator and report the final accumulated value
- Trace a FOR EACH loop with an IF inside and count how many elements meet a condition
- Identify what LENGTH(list) returns and use it in loop-count calculations
- Spot the 0-based vs 1-based indexing error
- Determine the contents of a new list built by APPEND inside FOR EACH
FAQ
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]