Java - While Loop

Java

In this tutorial we will learn about while loop in Java programming language.

What is a loop?

A loop is a block of code getting executed over and over again as long as the given condition is satisfied.

Why use a loop?

So, imagine we are given a task to write a program in Java to print "Hello World" 5 times.

To solve this we can print "Hello World" five times like the following.

class LoopExample {
  public static void main(String args[]) {
    System.out.println("Hello World");
    System.out.println("Hello World");
    System.out.println("Hello World");
    System.out.println("Hello World");
    System.out.println("Hello World");
  }
}

The code looks OK so far. But now lets say the requirement changed and we are now required to display "Hello World" 1,000,000 times!

If we use the above approach then it's going to take a while to code and it will be an inefficient way of solving the problem.

This is were we take help of a loop.

While syntax

while (condition) {
  // body of the loop
}

Where, condition is some condition and if it is satisfied then we execute the body of the while loop otherwise, we ignore it.

To come out of the loop we make the condition false by updating its value in the body of the loop. Or, we use the break statement to jump out of the loop.

Example #1: Write a program in Java to print "Hello World" ten times using while loop

To solve this problem we will use a counter variable and initialise it with integer value 1.

We will execute the body of the while loop till counter <= 10.

class LoopExample {
  public static void main(String args[]) {
    int counter = 1;

    while (counter <= 10) {
      System.out.println("Hello World");

      counter++;
    }
  }
}

Output:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Explanation:

So, we start with counter = 1; and the condition for the while loop is counter <= 10. This means we will execute the code inside the body of the while loop as long as counter is less than or equal to 10.

Inside the body of the loop we are printing "Hello World" using the println() method.

We are also incrementing the value ofcounter by 1 using the ++ increment operator.

When the value of counter variable becomes 11 then the while loop condition fails and so, we come out of the loop.

Example #2: Write a program in Java to print 1 to 10 using while loop but quit if multiple of 7 is encountered

In the following example we are using the while loop to print integer value from 1 to 10.

But we will jump out of the while loop if the value is a multiple of 7.

class LoopExample {
  public static void main(String args[]) {
    int counter = 1;

    while (counter <= 10) {

      System.out.println(counter);

      if (counter % 7 == 0) {
        System.out.println("Multiple of 7 found. So, quitting the while loop.");
        break;
      }

      counter++;
    }

    System.out.println("End of program.");
  }
}

Output:

1
2
3
4
5
6
7
Multiple of 7 found. So, quitting the while loop.
End of program.