The Stacks
UNIT 2: SELECTION AND ITERATION · TOPIC 2.7

2.7 while Loops

A while loop repeats as long as its condition is true. The exam asks how many iterations happen and what the variables hold when it stops.

What you need to know

  • while (condition) { body } — the condition is checked before each iteration. If it's false initially, the body runs zero times.
  • Something in the body must eventually make the condition false, or the loop is infinite.
  • Typical structure: initialize before the loop; test; body; update. Forgetting the update is the classic infinite loop.
  • After the loop, variables keep their final values. If a loop runs while i < 5 incrementing i, then i is 5 afterward.
  • while loops are the choice when the number of iterations isn't known in advance (reading input until a sentinel, searching until found).
  • An if inside a while with a return is a common way to exit a search loop early.

Worked example

int n = 20;
int count = 0;
while (n > 1)
{
    n = n / 2;
    count++;
}
System.out.println(n + " " + count);
check n > 1n aftercount
20: true101
10: true52
5: true23
2: true14
1: false → exit

Output: 1 4.

Trace it yourself

Step through with the buttons, or use the ← → keys. Changed variables are highlighted.

Exam tip: Always trace with a table and include a row for the final failed check — that's where students stop one iteration early or late. And check whether the condition is true at the start; "zero iterations" is a real answer choice.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • A while loop has four parts, even when they aren't adjacent: initialization (before), condition (header), body, update (in the body). Identify all four before tracing. A missing update is an infinite loop.
  • The condition is checked before every iteration, including the first. Zero iterations is a real outcome. The exam includes loops whose condition is false at the start.
  • Iteration count vs. final value: the body runs once for each check that passes. The variable's final value is the one that made the check fail. If the body runs for i = 3, 7, 11, then the loop stops with i = 15 — four values, three iterations.
  • Sentinel loops read input until a special value: while (n != -1) { … n = input.nextInt(); }. The sentinel isn't processed. The first read happens before the loop.
  • Search loops often have compound conditions: while (i < arr.length && arr[i] != target). The bounds check comes first (short-circuit). After the loop, i < arr.length tells you whether it was found.
  • Modifying the condition's variable in unexpected ways — dividing, multiplying, non-uniform steps — is how the exam varies these. Trace with a table; don't assume a formula.
  • A return inside a while loop (in a method) exits the loop and the method at once. Common in "find the first…" methods.

Mistakes that cost points

  • Counting the failed check as an iteration. Body runs only when the check passes.
  • Reporting the last-processed value as final. Final is one update past that.
  • Missing the zero-iteration case. Check the condition against the initial values first.
  • Assuming the update is +1. Read the body.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 How many times is "x" printed?
int k = 3;
while (k < 12)
{
    System.out.print("x");
    k += 4;
}
  1. A 2
  2. B 3
  3. C 4
  4. D 0
Show answer

Answer: B. k = 3 (print), 7 (print), 11 (print), 15 (stop). Three times.

Q2 What is the value of i after the following loop?
int i = 0;
while (i * i < 50)
{
    i++;
}
  1. A 7
  2. B 8
  3. C 49
  4. D 50
Show answer

Answer: B. 7 * 7 = 49 < 50, so i becomes 8. 8 * 8 = 64 is not < 50, so the loop stops with i = 8.

Q3 Which of the following loops runs forever?
  1. A int x = 10; while (x > 0) { x -= 3; }
  2. B int x = 1; while (x != 10) { x += 2; }
  3. C int x = 0; while (x < 5) { x++; }
  4. D int x = 8; while (x > 1) { x /= 2; }
Show answer

Answer: B. x takes odd values 1, 3, 5, 7, 9, 11, … and never equals 10.

Key vocabulary

while loop
repeats its body as long as the condition is true; condition checked first
Infinite loop
a loop whose condition never becomes false
Sentinel
a special input value that signals a loop to stop