6m left·0%
Reading Time: 6 min
Last Updated: February 26, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: February 26, 2026
Main Ideas: 5

Topic 2.7 Notes – while Loops

Verified for 2027 AP® Computer Science A Exam
Read aloud
While loops let you repeat code as long as a condition stays true. They are the most basic form of iteration in Java and are used when a process needs to continue until something changes. Understanding exactly when the condition is checked and how the loop stops is the key to getting these right on quizzes and the AP exam.

1. What a While Loop Is

A while loop is a pre-test loop. That means the Boolean condition is checked before each iteration, including the first one.

while (condition) {
    // loop body
}

Here’s the mental model. Each time through, the program checks the condition first. If it’s true, the loop body runs and control jumps back to test the condition again. If it’s false, the loop ends immediately.

Study guide illustration

What this means in practice:

  • The loop runs zero or more times.
  • If the condition is false at the start, the body never runs.
  • As soon as the condition becomes false, the loop stops.
  • If the condition is always true, you get an infinite loop.

Example of zero iterations:

int x = 8;

while (x < 5) {
    System.out.println("Hello");
}

System.out.println("Done");

"Hello" never prints. "Done" does.

That “zero times” possibility is something the AP loves testing in trace questions.

2. The Loop Control Variable Pattern

Most while loops follow a 3-part structure. If one piece is missing, the loop probably breaks.

a. Initialization (before the loop)

int count = 1;

b. Condition (in the header)

while (count <= 4)

c. Update (inside the loop)

count++;

Full example:

int count = 1;

while (count <= 4) {
    System.out.println(count);
    count++;
}

Here it is all labeled together so you can see the three parts in one place.

Three-part loop control variable structure

Two big things to check:

  • The update must move toward stopping.
    • If condition is count <= 4, you must increase count.
    • If condition is count > 0, you must decrease count.
  • The update must actually execute each time through the loop.

Bad example (infinite loop):

int count = 3;

while (count > 0) {
    System.out.println(count);
    count++;   // wrong direction
}

count keeps growing. The condition never becomes false.

3. When You Use a While Loop

You use a while loop when the number of repetitions isn’t fixed ahead of time.

a. Accumulator Pattern

Used to build a total.

int sum = 0;
int n = 1;

while (n <= 5) {
    sum += n;
    n++;
}

sum is the accumulator. It changes each iteration.

This pattern shows up constantly in FRQs when you’re counting, totaling, or averaging.

b. Sentinel-Controlled Loop

Stops when a special value appears.

int input = 0;
int total = 0;

while (input >= 0) {
    total += input;
    input = input - 1;  // imagine new input here
}

The sentinel value is the value that ends the loop. Usually, you don’t want to process the sentinel itself. Watch for that detail in problems.

c. Search Until Found

Continue looping until something becomes true.

boolean found = false;
int num = 1;

while (!found) {
    if (num == 4) {
        found = true;
    }
    num++;
}

The loop stops once found becomes true.

4. Tracing While Loops on the Exam

When you see a while loop in a multiple choice question:

  1. Write down initial variable values.
  2. Check the condition.
  3. If true, run the entire loop body.
  4. Update variables.
  5. Go back to the condition.

Make a small table like this:

Iterationcountsum
Start10
121
233
...

Two things students mess up:

  • Forgetting the condition is checked before the first run.
  • Forgetting the final update that makes the condition false.

Sometimes the question asks for the final value of a variable, not the output. Track everything.

5. Common Mistakes That Lose Points

Infinite Loops

Happens when:

  • No update
  • Wrong update direction
  • Condition always true

If a program “freezes,” this is usually why.

Off-by-One Errors

Loop runs one time too many or too few.

Example:

int i = 1;

while (i < 5) {
    System.out.println(i);
    i++;
}

Prints 1 2 3 4 but not 5.

Changing < to <= changes the number of iterations. The AP loves answer choices that differ by exactly one iteration.

Updating in the Wrong Place

If the control variable changes outside the loop, the loop condition never changes.

The update must be inside the loop body.

Key Takeaways

A while loop checks its condition before every iteration, including the first.
The loop body may execute zero times if the condition starts false.
Every correct while loop needs initialization, a condition, and an update that moves toward stopping.
Infinite loops happen when the condition never becomes false.
Off-by-one errors usually come from < vs <= or incorrect starting values.
When tracing, simulate each iteration step-by-step and re-check the condition every time.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining