Java

While Loop In Java

With the aid of examples, you will learn about the while loop in Java in this article. The while loop executes a series of statements continuously until the given condition returns false, just like the for loop does.

Syntax of while loop

while(condition)
{
   statement(s); //block of code
}

The while loop’s body (the text inside in curly brackets) section of code runs repeatedly until the condition returns false.

Java while loop flowchart

In a while loop, the condition is first tested to see if it is true, and if it is, the statements inside the loop are then executed. After the while loop, the control exits when the condition returns false and moves to the following sentence.

Simple while loop example

A straightforward Java application is provided to show how to utilize the while loop. In this program, we are displaying the integer numbers starting with 10 in reverse order (as I is initialized as 10). We are using iā€” within the while loop’s body to decrease the value of i. Therefore, on each subsequent iteration of the while loop, if I is less than 1, the loop checks to see if I is greater than 1, and if it is, the code is executed; otherwise, the loop terminates. When the value of I is 1, the while loop’s final iteration checks the condition and returns false since I 1. As a result, the print command was not executed for i=1 and the loop was terminated.

class WhileLoopExample {
    public static void main(String args[]){
         int i=10;
         while(i>1){
              System.out.println(i);
              i--;
         }
    }
}

Output:

10
9
8
7
6
5
4
3
2

Infinite while loop

We are exhibiting the endless while loop in this example. If a loop never terminates, or if the condition it specifies never yields false, it is referred to be an endless loop.

The condition i>1 in the example below never returns false since I starts off with a value of 10, and i++ is used to raise i’s value throughout each iteration of the loop.

class WhileLoopExample2 {
    public static void main(String args[]){
         int i=10;
         while(i>1)
         {
             System.out.println(i);
              i++;
         }
    }
}

Here is one more illustration of an endless while loop. The true boolean value is used in lieu of the condition in the code below, which causes the loop to execute forever.

class JavaExample {
  public static void main(String args[]){
    int i=10;
    while(true)
    {
      System.out.println(i);
      i++;
    }
  }
}

Example: Iterating an array using while loop

The while loop is used in the example below to iterate through an array and output each entry.

class WhileLoopExample3 {
    public static void main(String args[]){
         int arr[]={2,11,45,9};
         //i starts with 0 as array index starts with 0 too
         int i=0;
         while(i<4){
              System.out.println(arr[i]);
              i++;
         }
    }
}

Output:

2
11
45
9

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button