Java while loop

Summary: In this tutorial, you will learn how to use the Java while loop statement to execute a block of code repeatedly as long as a specified condition is true.

Introduction to Java while loop

A while loop allows you to execute a block of code repeatedly as long as a certain condition remains true.

Here’s the basic syntax of the while loop:

while(condition) {
    // Code to be executed as long as the condition is true
}Code language: Java (java)

In this syntax, the condition is a boolean expression that determines whether the loop should continue or terminate.

Java evaluates the condition before each iteration. If the condition is true, the loop continues, otherwise, the loop exits.

Because the while loop statement evaluates the condition at the beginning of each iteration, the while loop is also known as a pretest loop.

Java while loop examples

Let’s take some examples of using the while loop.

1) Using a while loop to display numbers from 0 to 4

The following example uses the while loop to print out numbers from 0 to 4:

public class App {
    public static void main(String[] args) {
        int count = 0;
        while(count < 5){
            System.out.println(count);
            count++;
        }
    }
}Code language: Java (java)

Output:

0
1
2
3
4Code language: Java (java)

How it works.

First, declare a variable count and initialize its value to zero.

Second, use the while loop to print the value of the count variable as long as the count is less than 5.

Inside the loop, we increment the count variable by one in each iteration using count++. The loop will continue until the condition count < 5 becomes false. Once the count is 5, the loop exits.

Notice that if you forget to increase the count inside the loop, the condition count < 5 is always true. As a result, the loop will run forever. In this case, you’ll have an indefinite loop.

2) Using a while loop to calculate the sum of numbers

The following program uses a while loop to calculate the sum of all integers from 1 to 100:

public class App {
    public static void main(String[] args) {
        int count = 1, total = 0;

        while(count <= 100){
            total += count;
            count++;
        }
        
        System.out.println(total);
    }
}Code language: Java (java)

Output:

5050Code language: Java (java)

How it works.

  • First, declare the count variable and initialize its value to 1. Also, declare the total variable with the initial value of 0.
  • Second, use a while loop that adds up the count to the total as long as the count is less than or equal to 100.
  • Third, display the total.

Notice that this example is for demonstration purposes. If you know Math, you can calculate the total of all integers from 1 to n using the formula: n * (n + 1) / 2.

Here’s the program:

public class App {
    public static void main(String[] args) {
        int n = 100;
        int total = n * (n + 1) / 2;
        System.out.println(total);
    }
}Code language: Java (java)

Output:

5050Code language: Java (java)

Summary

  • Use a Java while loop to execute a block of code as long as a condition is true.