Home › Java › Java while Loop

Java while Loop

Last updated: 28 Sep 2026

A while loop repeats as long as its condition is true. do-while always runs the body once first.

int n = 3;
while (n > 0) {
    System.out.println(n);
    n--;
}

Make sure the condition eventually becomes false, or the loop never ends.