A while loop in Java is used to execute a block of code repeatedly as long as a specified condition evaluates to true. It is particularly useful when the number of iterations is not known beforehand and depends on dynamic conditions.
Syntax:
while (condition) {
// Code to be executed
}
Example:
int count = 0;
while (count < 5) {
System.out.println("Count is: " + count);
count++;
}
In this example, the loop will print the value of count from 0 to 4. The loop continues until the condition count < 5 becomes false.
