Learn Java
import com.learnjava.controlflow.WhileLoop;

while Loop in Java

@Concept

A while loop repeats its body for as long as its condition stays true, checking that condition before every iteration — including the very first one, so the body might not run even once if the condition starts out false. It's the natural choice whenever the number of iterations isn't known ahead of time and instead depends on some changing state, like reading input until a sentinel value appears.

Since nothing about a while loop's syntax updates its own condition automatically, something inside the body has to change a variable the condition depends on, or the loop runs forever. An infinite loop is almost always a while loop whose body forgot to update the thing it's checking.

@HowItWorks
1

The syntax

while (condition) { body } repeats the body for as long as condition stays true.

2

Condition checked first

Evaluated before every iteration, including the first, so the body can end up running zero times.

3

Nothing updates automatically

Unlike a for loop's built-in update step, a while loop relies entirely on its body to eventually make the condition false.

4

Best suited to unknown iteration counts

Used when how many times to loop depends on changing state, not a fixed number decided in advance.

5

The classic infinite-loop bug

Forgetting to update the variable the condition depends on inside the body means the condition never becomes false.

@Example
public class WhileLoopDemo {
public static void main(String[] args) {
int count = 3;
while (count > 0) {
System.out.println("Countdown: " + count);
count--;
}
System.out.println("Liftoff!");
}
}
Output
Countdown: 3
Countdown: 2
Countdown: 1
Liftoff!
1count starts at 3, and 3 > 0 is true, so the body runs, printing "Countdown: 3", then count becomes 2.
22 > 0 is still true, so the body runs again, printing "Countdown: 2", then count becomes 1.
31 > 0 is still true, so the body runs a third time, printing "Countdown: 1", then count becomes 0.
40 > 0 is now false, so the loop ends, and the final println outside the loop prints "Liftoff!" — four lines total.
@UseCases
Reading input repeatedly until the user types a specific sentinel value like "quit"
Retrying an operation until it succeeds or a maximum attempt count is reached
Processing a queue or stream of work until it's empty
Debugging an infinite loop caused by forgetting to update the loop's condition variable

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.