Java : while loop


Java while loop



Java while loop is used to execute statement(s) until a condition holds true. In this tutorial we will learn looping using Java while loop examples. First of all lets discuss while loop syntax:
while (condition(s)) {
// Body of loop
}
1. If the condition holds true then the body of loop is executed, after execution of loop body condition is tested again and if the condition is true then body of loop is executed again and the process repeats until condition becomes false. Condition is always evaluated to true or false and if it is a constant, For example while (c) { …} where c is a constant then any non zero value of c is considered true and zero is considered false.
2. You can test multiple conditions such as
while ( a > b && c != 0) {
 // Loop body
}
Loop body is executed till value of a is greater than value of b and c is not equal to zero.
3. Body of loop can contain more than one statement. For multiple statements you need to place them in a block using {} and if body of loop contain only single statement you can optionally use {}. It is recommended to use braces always to make your program easily readable and understandable.

Java while loop example

Following program asks the user to input an integer and prints it until user enter 0 (zero).
import java.util.Scanner;
 
class WhileLoop {
  public static void main(String[] args) {
    int n;
 
    Scanner input = new Scanner(System.in);
    System.out.println("Input an integer"); 
 
    while ((n = input.nextInt()) != 0) {
      System.out.println("You entered " + n);
      System.out.println("Input an integer");
    }
 
    System.out.println("Out of loop");
  }
}
Output of program:
Java while loop example program
Above program can be written in a more compact way as follows:
// Java while loop user input 
import java.util.Scanner;
 
class WhileLoop {
  public static void main(String[] args) {
    int n;
 
    Scanner input = new Scanner(System.in);
    System.out.println("Input an integer"); 
 
    while ((n = input.nextInt()) != 0) {
      System.out.println("You entered " + n);
      System.out.println("Input an integer");
    }
  }
}

Java while loop break program

Here we write above program but uses break statement. The condition in while loop here is always true so we test the user input and if its is zero then we use break to exit or come out of the loop.
import java.util.Scanner;
 
class BreakWhileLoop {
  public static void main(String[] args) {
    int n;
 
    Scanner input = new Scanner(System.in);
 
    while (true) {
      System.out.println("Input an integer");
      n = input.nextInt();
 
      if (n == 0) {
        break;
      }
      System.out.println("You entered " + n);
    }
  }
}

Java while loop break continue program

import java.util.Scanner;
 
class BreakContinueWhileLoop {
  public static void main(String[] args) {
    int n;
 
    Scanner input = new Scanner(System.in);
 
    while (true) {
      System.out.println("Input an integer");
      n = input.nextInt();
 
      if (n != 0) {
        System.out.println("You entered " + n);
        continue;
      }
      else {
        break;
      }
    }
  }
}
Whatever you can do with while loop can be done with for and do while loop.

Previous
Next Post »