🔀 Control Flow · Beginner

while loop in Java

Condition checked before each iteration; may run zero times.

🧩 The mysteryA loop is meant to stop when x reaches 10. x keeps growing: 1, 3, 5, 7, 9... and the program never ends. 10 is right there! Why does it never arrive?

Repeat while true

A while loop repeats its body as long as its condition is true: check, run, check, run... Something inside the body must eventually make the condition false, or the loop never ends.

int n = 3;
while (n > 0) {
    System.out.println(n);
    n--;
} // prints 3, 2, 1

Checked BEFORE each pass

The condition is tested before every pass, including the very first. If it's false from the start, the body runs zero times. Here 10 < 5 is false immediately, so only end prints.

int count = 10;
while (count < 5) {
    System.out.println(count);
    count++;
}
System.out.println("end");
🔮 Predict it

Trace it

What prints?

int i = 2;
while (i < 30) {
    i *= 2;
}
System.out.println(i);
  1. 16
  2. 30
  3. 32
  4. 64
Show the answer

i goes 2, 4, 8, 16, 32. At 16, 16 < 30 is still true, so one more pass doubles it to 32. Then 32 < 30 fails. The condition is only checked *between* passes, so the last pass can overshoot.

🔮 Predict it

Count the steps

How many times does the body run?

int n = 6, steps = 0;
while (n != 1) {
    n = (n % 2 == 0) ? n / 2 : 3 * n + 1;
    steps++;
}
System.out.println(steps);
  1. 6
  2. 8
  3. 9
  4. 16
Show the answer

n goes 6, 3, 10, 5, 16, 8, 4, 2, 1: that's 8 updates. The loop exits as soon as the check sees n == 1.

⚠️ The trap

The update that escaped

The i++ below sits outside the loop's braces. Inside, i stays 0 forever, i < 5 is always true, and the program prints 0 until you kill it. Forgetting (or misplacing) the update is a classic cause of infinite while loops.

int i = 0;
while (i < 5) {
    System.out.println(i);
}
i++; // never reached

Safer stop conditions

✗ Exact target
int x = 1;
while (x != 10) {
    x += 2;
}

x is always odd (1, 3, ... 9, 11...), so it skips 10 and never stops. Even after int overflow wraps around, it stays odd.

✓ Range target
int x = 1;
while (x < 10) {
    x += 2;
}

< stops as soon as x passes 10, whatever the step size.

💼 In the real world

In real projects

A while loop whose exit is never reached shows up in production as a server stuck at 100% CPU, a frozen app, or a request that never returns. Defensive habits: prefer range checks (<) over exact ones (!=), and for loops that wait on the outside world, add a maximum number of attempts or a timeout.

Key takeaways

  1. The condition is checked BEFORE each pass
  2. It may run zero times
  3. Forgetting to update the loop variable gives an infinite loop

💡 Like checking the fridge before each snack: if it's empty from the start, you never eat at all.

🤯 Did you know?

The "count the steps" loop is the Collatz problem from 1937. Nobody has ever proved it reaches 1 for every starting number, although computers have checked enormous ranges and it always has.

Practice questions

What does this print?

int count = 10;
while (count < 5) {
    System.out.println(count);
    count++;
}
System.out.println("end");
  1. 10 end
  2. end
  3. 5 end
Check your answer

end. 10 < 5 is false on the very first check, so the body never runs and only end prints.

What does this print?

int i = 1;
while (i < 20) {
    i *= 3;
}
System.out.println(i);
  1. 9
  2. 20
  3. 27
  4. 81
Check your answer

27. i goes 1, 3, 9, 27. When i is 9, 9 < 20 is still true, so the body runs once more and makes it 27. Then 27 < 20 fails.

Next: what if the body must run at least once, like showing a menu before asking "again?"