AP CSP For Each Loops

AP CSP Topics › 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.

LENIterations of FOR EACH = length of the list
1Index offset: AP lists start at position 1, not 0
0Effect of modifying the loop variable on the original list — none

Structure and List Variable

FOR EACH item IN [10, 20, 30, 40]: one iteration per element list: 10 20 30 40 item = 10 body runs item = 20 body runs item = 30 body runs item = 40 body runs Loop complete — 4 iterations for a 4-element list item takes each list value in order. Changes to item do NOT change the list.

The loop variable takes each value in order. Modifying item inside the loop does not change the original list element.

FOR EACH Iterates the List
What the loop does
  • 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
FOR EACH Does NOT
Common misunderstandings
  • 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

AP Pseudocode List Operations Reference
# APPEND(list, value) — adds value to end of list # REMOVE(list, i) — removes element at index i (1-based) # INSERT(list, i, value) — inserts value at position i # LENGTH(list) — returns number of elements # list[i] — accesses element at index i (1-based)

Code Trace Gauntlet

Trace 1 — Sum Accumulator
nums [4, 7, 2, 9] total 0 FOR EACH n IN nums total total + n DISPLAY(total)

What is total after the loop?

Output

22 n=4: total=4. n=7: total=11. n=2: total=13. n=9: total=22. Display: 22.

Trace 2 — Conditional Inside Loop
scores [85, 92, 67, 78, 95] count 0 FOR EACH s IN scores IF s 90 count count + 1 DISPLAY(count)

How many scores are ≥ 90?

Output

2 85≥90? F. 92≥90? T, count=1. 67≥90? F. 78≥90? F. 95≥90? T, count=2. Display: 2.

Trace 3 — Finding Maximum
vals [3, 8, 1, 5, 12, 4] maxVal vals[1] FOR EACH v IN vals IF v > maxVal maxVal v DISPLAY(maxVal)

What is the maximum value found?

Output

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.

Trace 4 — Building a New List
original [1, 2, 3, 4, 5] doubled [] FOR EACH x IN original APPEND(doubled, x * 2) DISPLAY(doubled)

What does doubled contain after the loop?

Output

[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

SPOT THE BUG — Modifying Loop Variable Doesn’t Change List
# Intended: double every element in nums nums [1, 2, 3] FOR EACH n IN nums n n * 2 DISPLAY(nums)
Bug Explained

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.

SPOT THE BUG — 0-Based Index Error
list [“a”, “b”, “c”] DISPLAY(list[0])
Bug Explained

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

1
AP lists start at index 1, not 0

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.

2
Modifying the loop variable does not change the list

The loop variable is a copy of the element. Assigning a new value to it has no effect on the original list.

3
FOR EACH on an empty list runs zero times

If the list has no elements, the loop body never executes. The accumulator retains its pre-loop value.

4
LENGTH(list) is always tested as the number of elements

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+5+1+8+3 = 19.

2. items ← [10, 20, 30, 40]
DISPLAY(LENGTH(items))


What is displayed?

  • 3
  • 4
  • 40
  • 10
LENGTH returns the number of elements: 4.

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
FOR EACH runs once per element. 6 elements = 6 iterations.

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
n=3: 3>4? F. n=7: 7>4? T, count=1. n=2: F. n=9: 9>4? T, count=2. n=1: F. Display: 2.

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
AP pseudocode lists are 1-indexed. The first element is always at index 1.

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
I is correct. III is correct — no elements = no iterations. II is false — the loop variable is a copy.

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
String concatenation with + joins without spaces. result = “” + “cat” + “dog” + “bird” = “catdogbird”.

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.
Initializing minVal to 0 when all list values are positive means the condition (v < minVal) is always FALSE. minVal stays 0. Fix: initialize to the first element (list[1]) or to a very large number.

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
Modifying item does not change myList. The loop variable is a copy. myList remains [1, 2, 3].

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]
Values >4: 5 (TRUE), 3 (F), 8 (T), 1 (F), 9 (T), 2 (F). Appended: 5, 8, 9. Display: [5, 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

Can FOR EACH be used to modify list elements?
Not directly. The loop variable is a copy. To modify the list, you must use index-based access: list[i] ← newValue. FOR EACH is read-only traversal.
How does AP pseudocode handle list indices?
1-based: first element is list[1], last is list[LENGTH(list)]. This differs from Python (0-based) and Java (0-based). The AP exam expects 1-based indexing.
What happens when APPEND is called during a FOR EACH loop?
Adding elements to the list while iterating it can cause unexpected behavior. The AP exam generally does not test this edge case — APPEND questions usually involve building a new separate list.

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]