Monday, June 17, 2019

Java while Loop With Examples

In Java programming language there are three types of loops- while loop, do while loop and for loop. In this post we’ll learn about while loop in Java along with usage examples.

while loop in Java

While loop repeatedly executes a statement or a block of statements while its condition is true.

Syntax of the while loop in Java is as follows-

 
while (condition) {
  // loop body
}

Here condition is a boolean expression that evaluates to either true or false.

while loop body is repeatedly executed as long as the conditional expression is true. When condition evaluates to false, control comes out of the loop. The curly braces are not necessary if loop body consists of a single statement.

Java while loop execution flow

while loop in Java

Java while loop examples

1- Using while loop to print numbers 1..5.

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

Output

 
1
2
3
4
5

The condition (i <= 5) is evaluated first in the while loop if it’s true loop body is executed and in each iteration condition is evaluated to verify whether it is true or false.

In the loop body value of i is incremented by 1 in each iteration so that the condition eventually evaluates to false when i is greater than 5.

2- Using while loop in Java to print table of 7.

 
public class WhileLoopDemo {
  public static void main(String[] args) {
    int number = 7;
    int i = 1;
    while(i <= 10){
      System.out.println(number + " X " +i + " = " + (i*7));
      i++;
    }
  }
}

Output

 
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

Java while loop with no loop body

You can have a while loop with no loop body. Consider the following example where while loop is repeated as long as i is not greater than 10.

 
public class WhileLoopDemo {
  public static void main(String[] args) {
    int i = 1;
    while(!(++i > 10));
    System.out.println("Value of i- " + i);
  }
}

Output

 
Value of i- 11

Infinite while loop

Since while loop is evaluated as long as the condition remains true so you can have an infinite while loop by using condition as true. Consider the following example which has to be terminated manually to come out of an infinite while loop.

 
public class WhileLoopDemo {
  public static void main(String[] args) {
    while(true){
      System.out.println("This is an infinite while loop");
    }
  }
}

Usually you use such an infinite while loop with a break statement along with a condition to break out of loop.

 
public class WhileLoopDemo {
  public static void main(String[] args) {
    int i = 1;
    while(true){
      i++;
      // condition to break out of loop
      if(i > 20)
        break;
    }
    System.out.println("Value of i- " + i);
  }
}

Output

 
Value of i- 21

Same program can also be written using a boolean flag to control the while loop.

 
public class WhileLoopDemo {
  public static void main(String[] args) {
    boolean flag = true;
    int i = 1;
    while(flag){
      i++;
      // condition to turn flag to false
      if(i > 20)
        flag = false;
    }
    System.out.println("Value of i- " + i);
  }
}

Output

 
Value of i- 21

That's all for this topic Java while Loop With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Ternary Operator in Java
  2. Difference Between equals() Method And equality Operator == in Java
  3. Why no Multiple Inheritance in Java
  4. Package in Java
  5. Class in Java

You may also like-

  1. this Keyword in Java With Examples
  2. Java Exception Handling Tutorial
  3. StringBuffer Class in Java
  4. How HashMap Internally Works in Java
  5. Race Condition in Java Multi-Threading
  6. Factorial Program in Java
  7. Magic Methods in Python With Examples
  8. Spring JavaConfig Example Program

No comments:

Post a Comment