---
title: "Infinite Loop — AP Comp Sci A Definition & Exam Guide"
description: "An infinite loop is a loop whose condition never becomes false, so it runs forever. Learn how AP CSA tests it with while loops, ArrayList traversals, and tracing."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/infinite-loop"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 2"
---

# Infinite Loop — AP Comp Sci A Definition & Exam Guide

## Definition

An infinite loop is a loop whose Boolean condition never evaluates to false, so the loop body repeats forever and the program never moves past it. In AP CSA, it usually happens when a while loop's counter variable is never updated inside the loop body.

## What It Is

An infinite loop is a [loop](/ap-comp-sci-a/key-terms/loop "fv-autolink") that never stops because the [loop condition](/ap-comp-sci-a/key-terms/loop-condition "fv-autolink") never becomes false. Every loop in Java keeps running as long as its condition is true. If nothing inside the loop body ever changes the values in that condition, the condition stays true forever and the program gets stuck.

The classic AP CSA version looks like this: you write `while (i < list.size())`, you do something with `list.get(i)`, and you forget the `i++` at the bottom. The [variable](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") `i` never changes, the condition never flips to false, and the loop spins forever. Your program doesn't crash with an error message. It just hangs. That's what makes infinite loops sneaky compared to exceptions, which at least tell you something went wrong. (When it happens on your own machine, Ctrl+C is how you kill the runaway program.)

## Why It Matters

Infinite loops live at the heart of [Unit 4](/ap-comp-sci-a/unit-4 "fv-autolink") (Iteration) and come back hard in Units 6-8 when you [traverse](/ap-comp-sci-a/key-terms/traverse "fv-autolink") arrays, ArrayLists, and 2D arrays. The CED expects you to determine what a loop does and how many times its body executes, and "it never terminates" is one of the possible answers. Understanding why a loop fails to terminate forces you to actually understand how loops work, meaning the relationship between the condition, the loop body, and the update step. Every correctly written loop needs all three. An infinite loop is what you get when the update step is missing or can never make the condition false. On FRQs, writing an accidental infinite loop in your traversal code is one of the most common ways to lose points on otherwise correct logic.

## Connections

### [Loop Condition (Unit 4)](/ap-comp-sci-a/key-terms/loop-condition)

The loop condition is the on/off switch for [iteration](/ap-comp-sci-a/unit-2/while-loops/study-guide/7qGsGOh1UKALAWpJhZOi "fv-autolink"). An infinite loop is just a loop condition that can never reach false, either because the variables in it never change or because the update moves them in the wrong direction.

### [Indexed For Loop (Unit 4)](/ap-comp-sci-a/key-terms/indexed-for-loop)

A standard for loop bakes the [initialization](/ap-comp-sci-a/key-terms/initialization "fv-autolink"), condition, and update into one header, which is exactly why it rarely goes infinite. While loops are riskier because the update is your job, and forgetting it is the number one cause of infinite loops on the exam.

### [IndexOutOfBoundsException (Units 6-7)](/ap-comp-sci-a/key-terms/indexoutofboundsexception)

These are the two classic [traversal](/ap-comp-sci-a/key-terms/traversal "fv-autolink") failures, and they're mirror images. Loop too far and Java throws an exception that crashes the program. Never advance your index at all and you get an infinite loop that hangs instead. Knowing which mistake produces which symptom is a frequent MCQ move.

### Fixed Number of Repetitions (Unit 4)

MCQs love asking how many times a loop body executes. The answer choices often include a specific count, a count that's off by one, and "the loop never terminates." Tracing the loop variable by hand is how you tell a fixed repetition count apart from an infinite loop.

## On the AP Exam

Infinite loops show up most often in multiple choice questions that hand you a code segment and ask what it prints or how many times the body runs. One of the answer choices is frequently some version of "the loop never terminates," and you have to trace the loop variable to see whether the condition can ever become false. A common stem asks about the risks of using a while loop to traverse an ArrayList, where forgetting to increment the index creates an infinite loop (and removing elements while traversing can cause similar trouble). On FRQs, you won't be asked to write an infinite loop on purpose. The skill being tested is the opposite. Every loop you write must terminate, so double-check that your while loops update their control variable inside the body before you move on.

## Infinite Loop vs IndexOutOfBoundsException

Both are loop traversal bugs, but they behave completely differently. An IndexOutOfBoundsException happens when your index goes past the valid range (like using `<=` instead of `<` with size()), and Java crashes the program with an error message. An infinite loop happens when your index never advances at all, so the program doesn't crash. It just runs forever with no error. Crash means you went too far; hang means you never moved.

## Key Takeaways

- An infinite loop occurs when a loop's condition never evaluates to false, so the loop body repeats forever and the program hangs without crashing.
- The most common cause in AP CSA is forgetting to update the loop control variable inside a while loop, like leaving out i++ when traversing an ArrayList.
- While loops are more prone to infinite loops than for loops because the for loop header forces you to write the update step.
- An infinite loop hangs silently, while an IndexOutOfBoundsException crashes with an error. They are opposite traversal mistakes.
- On multiple choice questions, trace the loop variable through each iteration to check whether the condition can ever become false before picking "the loop never terminates."
- On FRQs, always confirm that every while loop you write changes the variables in its condition, because an infinite loop in your solution costs points.

## FAQs

### What is an infinite loop in AP Computer Science A?

An infinite loop is a loop whose condition never becomes false, so the body repeats forever and the program never progresses. In AP CSA it usually comes from a while loop that never updates its control variable.

### Does an infinite loop throw an exception in Java?

No. An infinite loop doesn't produce any error message. The program just keeps running and appears frozen. That's different from an IndexOutOfBoundsException, which immediately crashes the program with an error.

### How is an infinite loop different from an IndexOutOfBoundsException?

An infinite loop means your index never advances, so the program hangs forever with no error. An IndexOutOfBoundsException means your index went past the valid range (for example, accessing index equal to size()), and Java crashes the program. One never moves, the other moves too far.

### What causes an infinite loop in a while loop?

The variables in the loop condition never change in a way that makes the condition false. The classic case is `while (i < list.size())` with no `i++` inside the body, so `i` stays at 0 forever.

### How do I stop an infinite loop while testing my code?

Press Ctrl+C in the terminal (or hit the stop button in your IDE) to kill the program. Then check your loop body and make sure the loop control variable actually gets updated toward making the condition false.

## Related Study Guides

- [2.7 While Loops](/ap-comp-sci-a/unit-2/while-loops/study-guide/7qGsGOh1UKALAWpJhZOi)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/infinite-loop#resource","name":"Infinite Loop — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/infinite-loop","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/infinite-loop#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T00:50:33.605Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/infinite-loop#term","name":"Infinite Loop","description":"An infinite loop is a loop whose Boolean condition never evaluates to false, so the loop body repeats forever and the program never moves past it. In AP CSA, it usually happens when a while loop's counter variable is never updated inside the loop body.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/infinite-loop","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is an infinite loop in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"An infinite loop is a loop whose condition never becomes false, so the body repeats forever and the program never progresses. In AP CSA it usually comes from a while loop that never updates its control variable."}},{"@type":"Question","name":"Does an infinite loop throw an exception in Java?","acceptedAnswer":{"@type":"Answer","text":"No. An infinite loop doesn't produce any error message. The program just keeps running and appears frozen. That's different from an IndexOutOfBoundsException, which immediately crashes the program with an error."}},{"@type":"Question","name":"How is an infinite loop different from an IndexOutOfBoundsException?","acceptedAnswer":{"@type":"Answer","text":"An infinite loop means your index never advances, so the program hangs forever with no error. An IndexOutOfBoundsException means your index went past the valid range (for example, accessing index equal to size()), and Java crashes the program. One never moves, the other moves too far."}},{"@type":"Question","name":"What causes an infinite loop in a while loop?","acceptedAnswer":{"@type":"Answer","text":"The variables in the loop condition never change in a way that makes the condition false. The classic case is `while (i < list.size())` with no `i++` inside the body, so `i` stays at 0 forever."}},{"@type":"Question","name":"How do I stop an infinite loop while testing my code?","acceptedAnswer":{"@type":"Answer","text":"Press Ctrl+C in the terminal (or hit the stop button in your IDE) to kill the program. Then check your loop body and make sure the loop control variable actually gets updated toward making the condition false."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 2","item":"https://fiveable.me/ap-comp-sci-a/unit-2"},{"@type":"ListItem","position":4,"name":"Infinite Loop"}]}]}
```
