July 19, 2022

while Loop in Java With Examples

while loop in Java repeatedly executes a block of statement while the given condition is true. Syntax of the while loop in Java is as follows-

while (condition) {
  // body
}

Block of code that is executed with in a loop is enclosed in curly braces. If only a single statement is executed with in while loop then curly braces are optional.

Java while loop execution flow

Following image shows the execution flow of the while loop.

The while statement evaluates the condition which is a boolean expression and it must return a boolean value. If the boolean expression evaluates to true, then block of code in the while loop is executed.

The while statement continues testing the expression and executing its block until the expression evaluates to false. When condition controlling the while loop becomes false, loop is terminated and control passes to the next line of code immediately following the loop.

One thing to note about the while loop is that the conditional expression is evaluated at the top of the loop so the code with in the loop will not execute even once if the condition is evaluated to false in the beginning itself. That's how while loop differs from do-while loop.

Java while loop examples

1- First example uses while loop to print numbers from 1 to 10.

public class WhileDemo {
  public static void main(String[] args) {
    int i = 1;
    while(i <= 10){
      System.out.println("Value- " + i);
      i++;
    }
  }
}
Output
Value- 1
Value- 2
Value- 3
Value- 4
Value- 5
Value- 6
Value- 7
Value- 8
Value- 9
Value- 10

In the while loop condition (i <= 10) is evaluated in each iteration, it returns true till value of i is less than or equal to 10. Condition is evaluated to false when value of i becomes greater than 10 and the loop terminates.

Value of i is incremented with in the while loop body so that the condition eventually evaluates to false.

2- Second example uses while loop to print numbers in reverse order 10 to 1.

public class WhileDemo {
  public static void main(String[] args) {
    int i = 10;
    while(i > 0){
      System.out.println("Value- " + i);
      i--;
    }
  }
}
Output
Value- 10
Value- 9
Value- 8
Value- 7
Value- 6
Value- 5
Value- 4
Value- 3
Value- 2
Value- 1

3- A while loop is executed repeatedly till the condition is true so you can implement an infinite loop by using a while(true) loop. Here is an example of while(true) loop in Java. You will need to manually terminate the code to come out of the loop.

public class WhileDemo {
  public static void main(String[] args) {
    while(true){
      System.out.println("In while loop running infinitely ");
    }
  }
}

4- Since a condition controlling the while loop is a boolean expression so you can use a boolean flag to control the loop. Following example shows another way to display numbers 1 to 10 using while loop and a boolean flag.

public class WhileDemo {
  public static void main(String[] args) {
    int i = 0;
    boolean done = false;
    while(!done){
      System.out.println("value- " + ++i);
      if(i == 10)
        done = true;
    }
  }
}

That's all for the topic while Loop in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment