Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

Infinite Loop in AP Computer Science A

In AP Computer Science A, an infinite loop happens when the Boolean expression controlling an iterative statement, like a while loop, always evaluates to true. The loop body keeps repeating and the program never moves past the loop (EK 2.7.A.2).

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is Infinite Loop?

An infinite loop is a loop that never stops. The CED defines it precisely. An infinite loop occurs when the Boolean expression in an iterative statement always evaluates to true (EK 2.7.A.2). A while loop checks its condition before every pass through the loop body, including the first one (EK 2.7.B.1). It only quits when that condition turns false. If nothing inside the loop ever pushes the condition toward false, the loop just keeps going.

Think of a while loop as a bouncer who checks your ticket every single time you walk back in. An infinite loop is a ticket that never expires. On the AP exam, the cause is almost always one of three things. The loop variable is never updated. It's updated in the wrong direction. Or it's updated in a way that gets stuck on the same value. The fix is usually one line, the line that's supposed to move the loop closer to ending.

Why Infinite Loop matters in AP® Computer Science A

Infinite loops live in Topic 2.7 While Loops in Unit 2: Selection and Iteration. They tie directly to learning objective 2.7.A (identify when an iterative process is required), which names infinite loops in EK 2.7.A.2. They also tie to 2.7.B (develop while loops and determine their results), because you can't figure out what a loop returns until you know whether it ever stops. Tracing loop behavior is one of the most heavily tested skills in AP CSA. A question that asks "what does this method return?" sometimes has the honest answer "nothing, it never terminates." Spotting that fast saves you from tracing forever on test day.

How Infinite Loop connects across the course

Loop condition (Unit 2)

The loop condition is the Boolean expression the while loop checks before every pass. An infinite loop is what you get when that condition can never become false. If you want to know whether a loop ends, look at the condition and ask what variable it depends on.

Loop body and the update step (Unit 2)

The loop body has to change something the condition depends on. If the body never touches the loop variable, or changes it the wrong way, the condition stays true forever. Most infinite loop bugs on the exam come down to one broken update line inside the body.

Off by one error (Unit 2)

Both are loop bugs from EK 2.7.A, but they differ in severity. An off by one error stops one iteration too early or too late (EK 2.7.A.4). An infinite loop never stops at all. Same family of mistake, very different outcome.

Fixed number of repetitions (Unit 2)

Loops built to run a fixed number of times, like a counter that goes up by 1 until it hits a limit, are hard to make infinite by accident. While loops that depend on data, like dividing a number down or searching a string, are where infinite loops sneak in. The more the stopping point depends on changing data, the more carefully you should check the update.

Is Infinite Loop on the AP® Computer Science A exam?

Infinite loops show up mostly in multiple-choice questions that hand you a while loop and ask what it does. Expect two styles.

  • Vocabulary stems: A loop keeps running because a variable never changes value. Which term describes this error? The answer is infinite loop.
  • Buggy method tracing: You get a method with a planted bug and have to figure out what happens. A classic example is a sumDigits method that writes n = n % 10; where it should write n = n / 10;. For an input like 23, n becomes 3, and 3 % 10 is still 3. So n > 0 stays true forever. A string-counting loop using indexOf is another favorite. If idx never gets reassigned inside the loop, idx != -1 never changes.

The skill is to find the loop condition, find what variable it depends on, and check whether the body actually moves that variable toward making the condition false. Watch out for distractors that look similar but aren't infinite loops. A maxPowerOfTwo loop using power < n does stop, but it can return the wrong value. That's a logic error, not an infinite loop. No released FRQ has used this term verbatim. On FRQs, though, writing a while loop that never terminates means your method doesn't work, so always double-check your update step.

Infinite Loop vs Off by one error

Both come from EK 2.7.A, and both are loop mistakes, but they fail in different ways. An off by one error means the loop does stop, just one iteration too many or too few (EK 2.7.A.4). You get a wrong answer. An infinite loop means the condition always evaluates to true (EK 2.7.A.2), so the loop never stops and the method never returns. Quick test: if you can count how many times the loop ran, it's not infinite.

Key things to remember about Infinite Loop

  • An infinite loop occurs when the Boolean expression controlling a loop always evaluates to true, so the loop never terminates (EK 2.7.A.2).

  • A while loop checks its condition before every iteration, including the first, and only stops once that condition becomes false.

  • Most infinite loops come from a missing or broken update, where the loop body never changes the variable the condition depends on in the right direction.

  • A loop whose condition is false at the start runs zero times, which is the opposite of an infinite loop (EK 2.7.A.3).

  • An off by one error runs one time too many or too few but still stops, while an infinite loop never stops at all.

  • To check whether a loop ends, find the variable in the condition and confirm the loop body moves it toward making the condition false.

Frequently asked questions about Infinite Loop

What is an infinite loop in AP Computer Science A?

An infinite loop is a loop whose Boolean expression always evaluates to true, so the loop body repeats forever and the program never gets past it. The CED states this in EK 2.7.A.2 under Topic 2.7 While Loops.

Is an infinite loop the same as an off by one error?

No. An off by one error still terminates but runs one iteration too many or too few (EK 2.7.A.4). An infinite loop never terminates at all because its condition never becomes false.

Does a while loop with a false condition run forever?

No, it runs zero times. A while loop checks its condition before the first iteration, so if the condition starts out false, the loop body never executes (EK 2.7.A.3).

How do you spot an infinite loop in an AP CSA multiple-choice question?

Find the variable in the loop condition, then check whether the loop body changes it in a way that eventually makes the condition false. For example, in a digit-sum loop, n = n % 10; gets stuck on the same nonzero value, so n > 0 stays true. The correct update is n = n / 10;.

Is infinite loop on the AP Computer Science A exam?

Yes. It's named directly in the CED under learning objective 2.7.A (EK 2.7.A.2). It shows up in multiple-choice questions where you trace a buggy while loop and decide whether it ever terminates.