What is the purpose of a while loop in Java?

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.

0 Comments

no data
Be the first to share your comment!