Skip to main content
← AP CSA Study Guide

Unit 3 · Heavy

Loops and Iteration

Repetition, and the accounting that makes it correct. The unit is less about loop syntax than about knowing what each variable means after one pass, which is the only reliable way to get bounds and accumulators right.

What a strong answer looks like

A strong Unit 3 answer states what the accumulator holds after one iteration, then shows the loop bound is consistent with that meaning.

Topics in this unit

1

For Loops

Practice this topic →

Know

A for loop packages initialization, condition, and update in one header. The condition is checked before every pass, so a loop can run zero times.

Apply

Count iterations by listing the first and last values the counter actually takes, rather than subtracting the bounds.

Watch out

Using less-than-or-equal against a length, which runs one pass too many and reads past the end.

Study move

For three loop headers, state the exact number of iterations before running anything.

2

While Loops

Practice this topic →

Know

A while loop separates the update from the header, which is what makes an infinite loop possible when the update is missing or unreachable.

Apply

Check that every path through the body moves the condition toward becoming false.

Watch out

An update placed inside a conditional branch that does not always execute.

Study move

Rewrite a for loop as a while loop and confirm both produce identical output.

3

Nested Loops

Practice this topic →

Know

The inner loop completes fully for each single pass of the outer loop, so total iterations multiply rather than add.

Apply

Name what each loop controls before tracing, then count inner executions as the product.

Watch out

Using the outer loop variable inside the inner body where the inner one was intended.

Study move

Trace a nested loop that prints only pairs where the first index is less than the second.

4

Accumulators and Counters

Practice this topic →

Know

An accumulator carries a running result across iterations. Its initial value must be the identity for the operation: zero for a sum, one for a product.

Apply

Write in words what the accumulator holds after processing index i, then check the initialization satisfies that for the empty case.

Watch out

Initializing a running maximum to zero, which fails whenever all values are negative.

Study move

Write a loop finding the largest value and state why the first element, not zero, is the safe initial value.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Counting iterations exactly rather than approximately
  • Stating the meaning of an accumulator before writing the loop
  • Recognising that a loop may legitimately run zero times

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • do-while loops. Taught in some classes; not required by the exam.
  • Enhanced for loops. Introduced here in some courses and with arrays in others.

Mastery checklist

  • State the exact iteration count for a given loop header.
  • Identify why a while loop fails to terminate.
  • Compute the total passes of a nested loop.
  • Choose a correct initial value for a sum, a product, and a maximum.

Check yourself

  • When does a for loop run zero times?
  • Why is zero an unsafe starting value for a running maximum?
  • How do total iterations change when a nested loop bound depends on the outer index?

Modeling drill

Write a loop that counts values above the average of an array, then state what it returns for an empty array.

Loop boundIteration countAccumulatorCounterNested loopInfinite loop