Loop Condition

A loop condition is the Boolean expression a loop evaluates before each iteration to decide whether to run the body again; the loop continues while the condition is true and stops the moment it evaluates to false.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is Loop Condition?

A loop condition is the Boolean expression sitting in the parentheses of a while or for loop. Before every single iteration, Java evaluates it. True means run the body one more time. False means skip the body and jump to the code after the loop.

Here's the mental shift that makes loops click. The condition is a continuation condition, not a stopping condition. while (count < 10) doesn't mean "stop at 10." It means "keep going as long as count is less than 10." Reading it backwards is the source of most loop bugs. Something inside the loop body (or the update part of a for loop, like i++) has to eventually push the condition to false, or the loop never ends. The condition is only checked at the top of each pass, so if it's false the very first time, the body runs zero times. That zero-iteration case shows up constantly in AP questions.

Why Loop Condition matters in AP Computer Science A

Loop conditions are the heart of Unit 4 (Iteration) in AP CSA, where you write and trace while and for loops. Almost every classic Unit 4 skill runs through the condition. Counting how many times a loop executes means asking "for which values is this condition true?" Spotting an infinite loop means asking "does anything ever make this condition false?" Off-by-one errors are almost always a < vs <= mistake in the condition.

It doesn't stay in Unit 4, either. Array and ArrayList traversals in Units 6-7 depend on conditions like i < arr.length (use <= and you get an ArrayIndexOutOfBoundsException). 2D array nested loops in Unit 8 stack two conditions on top of each other. Every FRQ that processes a list, string, or grid lives or dies by a correctly written loop condition.

How Loop Condition connects across the course

Infinite Loop (Unit 4)

An infinite loop is what happens when the loop condition never becomes false, usually because the loop body forgot to update the variable the condition depends on. On the exam, "which change causes an infinite loop?" is really a question about the condition.

Iteration (Unit 4)

Iteration is the act of repeating the loop body, and the loop condition is the gatekeeper that decides how many iterations happen. Tracing iteration questions means checking the condition before each pass, not after.

Fixed Number of Repetitions (Unit 4)

A for loop like for (int i = 0; i < n; i++) produces exactly n repetitions because the condition i < n is true for exactly n values of i. Counting executions is just counting the true cases of the condition.

Equals Method (Unit 2)

When a loop condition compares Strings, you need .equals() instead of ==, because == compares references, not contents. A condition like while (!word.equals("stop")) is correct; while (word != "stop") is a classic trap answer.

Is Loop Condition on the AP Computer Science A exam?

Loop conditions show up heavily in multiple choice as code-tracing questions. You'll be asked how many times a loop body executes, what a variable equals after the loop ends, or which version of a condition causes an infinite loop or an off-by-one error. The reliable move is to test the boundary values: the first value of the loop variable, the last value where the condition is true, and the value that finally makes it false.

On the FRQs, you write the conditions yourself. FRQ 1 (Methods and Control Structures) and FRQ 3-4 (array/ArrayList and 2D array questions) almost always require a traversal loop, and the rubric expects a condition that hits every needed element without going out of bounds. Writing i <= arr.length instead of i < arr.length costs points and crashes the code. No released FRQ asks you to define "loop condition," but nearly every one makes you write a correct one.

Loop Condition vs If-statement condition

Both are Boolean expressions in parentheses, but an if condition is checked exactly once, while a loop condition is re-checked before every iteration. An if statement asks "should I do this?" one time. A loop condition asks "should I do this again?" over and over until the answer is no. That repetition is why a loop condition needs something in the body to eventually change its outcome, and an if condition doesn't.

Key things to remember about Loop Condition

  • A loop condition is a Boolean expression checked before each iteration; the loop runs while it is true and exits as soon as it is false.

  • It's a continuation condition, not a stop condition, so while (x < 10) means 'keep going while less than 10,' not 'stop at 10.'

  • If the condition is false on the very first check, the loop body executes zero times, which is a favorite multiple-choice trap.

  • If nothing inside the loop ever makes the condition false, you get an infinite loop.

  • For array traversals, the safe condition is i < arr.length; using <= goes one index too far and throws an ArrayIndexOutOfBoundsException.

  • When the condition compares Strings, use .equals() instead of ==, because == compares references rather than contents.

Frequently asked questions about Loop Condition

What is a loop condition in AP Computer Science A?

It's the Boolean expression in the parentheses of a while or for loop that Java evaluates before each iteration. True runs the body again; false ends the loop. It's the core mechanic of Unit 4 (Iteration).

Does the loop body run at least once if the condition is false?

No. For the while and for loops tested on the AP exam, the condition is checked before the first iteration, so a false condition means the body runs zero times. Java's do-while loop runs once before checking, but do-while is not part of the AP CSA exam.

Is the loop condition checked after every iteration or before?

Before. The loop evaluates the condition at the top of each pass. The body might even change the variable mid-iteration, but the loop won't exit until it loops back up and re-checks the condition.

How is a loop condition different from an if condition?

An if condition is evaluated once and the program moves on. A loop condition is evaluated repeatedly, before every iteration, until it becomes false. That's why loops need an update statement (like i++) and if statements don't.

Why does i <= arr.length cause an error in a loop condition?

Array indexes run from 0 to arr.length - 1, so when i equals arr.length the condition is still true and arr[i] throws an ArrayIndexOutOfBoundsException. The correct condition for a full traversal is i < arr.length.