While loop

While loop

While loop — executes its block of code as long as the specified condition is true.

While (condition) {
    // Loop body
}
int number = 5;
int factorial = 1;

while (number > 0) {
    factorial *= number;
    number--;
}

System.out.println("Factorial: " + factorial);  // Output: Factorial: 120

It’s useful when the number of iterations is not known beforehand.

Related concepts

While loop

While loop — Structure map

Clickable & Draggable!

While loop — Related pages: