While loop
While loop
While loop — executes its block of code as long as the specified condition is true.
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.
Semantic portal