Home page

Tuesday, June 6, 2023

While loop in JAVA

 public class WhileLoopExample {

    public static void main(String[] args) {

        int count = 0;

        while (count < 5) {

            System.out.println("Count: " + count);

            count++;

        }

    }

}

In this example, we initialize a variable count to 0. The while loop will continue executing as long as the condition count < 5 is true. Inside the loop, we print the current value of count using System.out.println(), and then increment count by 1 using the count++ statement.

The loop will run for a total of 5 iterations, printing the values of count from 0 to 4. Once count reaches 5, the condition count < 5 becomes false, and the loop terminates.

No comments:

Post a Comment