AP Computer Science A Unit 4: Iteration
Study while loops, for loops, nested loops, loop analysis, String traversal with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
Iteration uses loops to repeat code. This unit covers while loops, for loops, and nested loops, along with common algorithms like finding a sum, maximum, or counting occurrences.
Why it matters
Loops are fundamental to every programming task from data processing to game logic. The AP CSA exam heavily tests loop tracing, off-by-one errors, and standard loop algorithms. Nested loops are essential for 2D arrays in Unit 8.
Key concepts
- A while loop repeats as long as its condition is true; the condition is checked before each iteration.
- A for loop combines initialization, condition, and update in one line: for(int i=0; i<n; i++).
- An infinite loop occurs when the condition never becomes false — usually a bug.
- Standard algorithms: sum, average, count, max/min, and searching.
While Loops
A while loop evaluates its condition before each iteration. If the condition is initially false, the body never executes. A common pattern is using a sentinel value to control the loop: while (input != -1). The loop body must eventually change something that affects the condition, or the loop runs forever. The AP exam tests loop tracing by asking how many times a loop executes or what value a variable holds after the loop completes. Always trace carefully, especially with off-by-one boundaries.
For Loops
A for loop packs initialization, condition, and update into one line: for (int i = 0; i < 10; i++). This is equivalent to a while loop but is more concise for counting iterations. The loop variable i typically starts at 0 and runs while i < length, which is the standard idiom for traversing arrays and Strings. Changing the start, condition, or increment alters which values are processed. For loops are the most common loop on the AP exam.
Nested Loops and Loop Algorithms
A nested loop places one loop inside another. The inner loop completes all its iterations for each single iteration of the outer loop. If the outer loop runs m times and the inner loop runs n times, the body executes m * n times total. Standard loop algorithms include computing a running sum, finding the maximum or minimum, counting items that meet a condition, and determining if any or all elements satisfy a property. These algorithms appear repeatedly on the AP exam in various contexts.
AP exam tip
When tracing a loop on the AP exam, make a table with columns for each variable and one row per iteration. This prevents mistakes and is especially helpful for nested loops.
Connections to other units
- Unit 2: String traversal uses loops with charAt() or substring() to process each character.
- Unit 6: Array traversal is the primary application of for loops.
- Unit 8: 2D array traversal requires nested for loops — one for rows and one for columns.