Java while Loop

In this lesson of our Java course, we will learn about Java while loop. In Java, loops are used to execute a block of code repeatedly until a certain condition is met. One of the loop types available in Java is the while loop.

Java while loops are a type of control flow statement that repeatedly executes a block of code, as long as a condition is true. There are 2 variations of the Java while loop. The main difference between a do-while loop and a while loop is that a do-while loop guarantees that the code block inside the loop will be executed at least once.

1. Java while loop

The while loop in Java is a type of control flow statement that repeatedly executes a block of code as long as a condition is true. The basic syntax of the while loop is:

while (condition) {
   // code block to be executed
}

1.1. How while loop works?

Let’s look at the following flowchart to understand how Java while loop works:

Java while loop
How Java While loop works?

1.2. While loop example

public class JavaWhileLoopExample {
    public static void main(String[] args) {
        int i = 0;
        while (i < 5) {
            System.out.println(i);
            i++;
        }
    }
}

In this example, the while loop will execute the code block that prints the value of i to the console and increments i by 1, as long as the condition i < 5 is true. The loop will exit when i is no longer less than 5.

2. Java do… while loop

The do-while loop in Java is like the while loop, but it guarantees that the code block inside the loop will be executed at least once. The basic syntax of the do-while loop is:

do {
   // code block to be executed
} while (condition);

2.1. How do-while Loop Works in Java

A flowchart of a do-while loop would look like this:

Java do while loop
How do while loop works in Java

2.2. do-while loop Example

public class JavaWhileLoopExample {
    public static void main(String[] args) {

        int i = 0;
        do {
            System.out.println(i);
            i++;
        } while (i < 5);
    }
}

In this example, the do-while loop will execute the code block that prints the value of i to the console and increments i by 1, at least once.

The do-while loops are useful when you want to execute a block of code at least once before checking the condition, for example, when you want to read user input until they enter the correct value

public class JavaWhileLoopExample {
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        int value;
        do {
            System.out.print("Enter a value between 1 and 10: ");
            value = input.nextInt();
        } while (value < 1 || value > 10);
    }
}

This example uses a do-while loop to read user input and check that the value entered is between 1 and 10. The code block inside the loop prompts the user to enter a value and reads the input using the nextInt() method of the Scanner class. The condition value < 1 || value > 10 is evaluated after the code block has been executed, and as long as the condition is true, the loop will continue to execute.

Summary

The while loops are a useful tool for controlling the flow of a program and repeatedly executing a block of code as long as a condition is true. The main difference between a do-while loop and a while loop is that a do-while loop guarantees that the code block inside the loop will be executed at least once. As always, the source code for this lesson is available on our GitHub repository.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.