In AP Computer Science A, a loop is an iteration statement that repeats a block of code zero or more times, as long as its controlling Boolean expression evaluates to true. The while loop (Topic 2.7) checks the condition before every iteration, including the first, and stops when the condition becomes false.
A loop is how Java repeats itself. Instead of copy-pasting the same line ten times, you write the code once inside a loop body and let a Boolean expression decide how many times it runs. Per EK 2.7.A.1, iteration statements change the flow of control by repeating a segment of code zero or more times while the condition is true.
The while loop is the foundational version. Before every single pass through the body (including the very first one), Java evaluates the Boolean expression. True means run the body again; false means skip past the loop and keep going (EK 2.7.B.1). That "check first" rule has two consequences you have to internalize. If the condition is false from the start, the body runs zero times (EK 2.7.A.3). And if the condition can never become false, you get an infinite loop that never stops (EK 2.7.A.2). The most common bug in between those extremes is the off by one error, where the loop runs one time too many or one time too few (EK 2.7.A.4).
Loops live in Unit 2 (Selection and Iteration), specifically Topic 2.7, and support two learning objectives. AP Comp Sci A 2.7.A asks you to recognize when a problem requires iteration, like counting every transaction over $1000 in a dataset of unknown size. AP Comp Sci A 2.7.B asks you to write while loops and, just as often, trace someone else's loop to determine exactly what it prints. Beyond Unit 2, loops are the engine behind almost everything else in the course. Traversing a String character by character, walking through an ArrayList, and processing a 2D array all come down to a loop with the right condition and the right update. If your loop logic is shaky, every later unit gets harder.
Keep studying AP® Computer Science A Unit 2
Off by One Error (Unit 2)
This is the signature loop bug the CED calls out by name (EK 2.7.A.4). Whether you write i < 4 or i <= 4 changes how many times the body runs, and MCQ answer choices are built around exactly that one-iteration difference.
Boolean Expressions and Selection (Unit 2)
A while loop is basically an if statement with a rewind button. Both gate a block of code behind a Boolean expression, but an if checks once and moves on, while a loop jumps back and checks again after every pass.
Infinite Loops (Unit 2)
If nothing inside the body ever changes the variables in the condition, the Boolean expression stays true forever and the program never terminates (EK 2.7.A.2). Always ask: what line in this body pushes the condition toward false?
Traversing Strings, ArrayLists, and 2D Arrays (later units)
Every traversal you do later in the course is a loop wearing a costume. The 2017 FRQs alone had you loop through a list of digits, a phrase String, and a 2D array of integers. The loop skills are identical; only the data structure changes.
Loops are tested two ways. On multiple choice, the most common stem is code tracing. You're given a while loop and asked what prints, like the segment that sums i * 2 while i <= 4, or the GCD loop that repeatedly computes a % b until b hits 0. The wrong answers are usually off by one (the loop ran one extra or one fewer time than you thought), so trace with a variable table, not in your head. Other MCQs ask you to identify when iteration is required at all, or to spot why a loop terminates early or never terminates. On the FRQs, you write loops constantly. The 2017 exam had you loop through an ArrayList of digits (Q1), modify a String inside a Phrase class (Q3), and reason about a 2D array (Q4), and 2018 Q2 needed loops to compare pairs of words. Botching the loop bounds costs points on otherwise correct logic, so always check your start value, your condition, and your update step.
Both an if statement and a while loop guard a block of code with a Boolean condition, and they even look similar on the page. The difference is repetition. An if checks the condition once, runs the block at most one time, and moves on. A while loop jumps back to recheck the condition after every pass, so the body can run zero, one, or many times. A quick tell when tracing: if the variable in the condition gets updated inside the body, the author intended repetition.
A loop repeats a segment of code zero or more times, as long as its controlling Boolean expression evaluates to true (EK 2.7.A.1).
A while loop checks its condition before every iteration, including the first, so if the condition starts out false the body never runs at all (EK 2.7.A.3, EK 2.7.B.1).
An infinite loop happens when the Boolean expression always evaluates to true, usually because nothing in the loop body changes the variables in the condition (EK 2.7.A.2).
An off by one error means the loop ran one time too many or one time too few, and it's the most common trap in both your own code and MCQ answer choices (EK 2.7.A.4).
When tracing a loop, write out a table of variable values after each iteration instead of trusting your head, because exam questions are designed to punish mental shortcuts.
Loops are the foundation for traversing Strings, ArrayLists, and 2D arrays later in the course, so loop bounds you master in Unit 2 pay off on almost every FRQ.
A loop is an iteration statement that repeats a block of code while a Boolean expression evaluates to true. In Topic 2.7 you learn the while loop, which evaluates its condition before each iteration and stops as soon as the condition is false.
No. The while loop checks its Boolean expression before the first iteration, so if the condition is false initially, the body executes zero times (EK 2.7.A.3). That zero-iteration case shows up in multiple choice answer options on purpose.
An if statement checks its condition once and runs its block at most one time. A loop rechecks the condition after every pass, so its body can execute zero, one, or many times. If the body updates a variable used in the condition, you're looking at a loop's logic, not a one-time check.
An infinite loop occurs when the Boolean expression always evaluates to true (EK 2.7.A.2), typically because the loop body never changes the variables the condition depends on. For example, while (x < 5) with no update to x inside the body will never terminate.
It's when a loop runs exactly one time too many or one time too few (EK 2.7.A.4), usually from mixing up < and <= in the condition or starting a counter at 0 versus 1. It's the single most common loop bug tested on the AP exam.
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
Review units, study guides, and course resources.
Check this vocabulary in multiple-choice context.
Apply key concepts in written AP responses.
Estimate the exam score you are working toward.
Review the highest-yield facts before practice.
Put the full course together before test day.