You are here

Java while loop

Java while loop is used to execute statement(s) until a condition is true. In this tutorial, we will learn to use while loop with examples. First of all, let's discuss while loop syntax:

while (condition(s)) {
// Body of loop
}

1. If the condition(s) holds true then the body of the loop is executed, after execution of the loop body condition is tested again and if the condition is still true then the body of the loop is executed again, and the process repeats until the condition(s) becomes false. The condition evaluates 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 to be true, and zero is considered false.

2. You can test multiple conditions such as

  1. while (a > b && c != 0) {
  2.  // Loop body
  3. }

Loop body is executed till value of variable a is greater than value of variable b and variable c isn't equal to zero.

3. A body of a loop can contain more than one statement. For multiple statements, you need to place them in a block using {}, and if the body of a loop contains only one statement, you can optionally use {}. It is always recommended to use braces to make your program easy to read and understand.

Java while loop example

Following program asks a user to input an integer and prints it until the user enter 0 (zero).

  1. import java.util.Scanner;
  2.  
  3. class WhileLoop {
  4.   public static void main(String[] args) {
  5.     int n;
  6.    
  7.     Scanner input = new Scanner(System.in);
  8.     System.out.println("Input an integer");
  9.    
  10.     while ((n = input.nextInt()) != 0) {
  11.       System.out.println("You entered " + n);
  12.       System.out.println("Input an integer");
  13.     }
  14.    
  15.     System.out.println("Out of loop");
  16.   }
  17. }

Output of program:
Java while loop example program


Java while loop break program

We can write above program using a break statement. We test a user input and if it is zero then we use "break" to exit or come out of the loop.

  1. import java.util.Scanner;
  2.  
  3. class BreakWhileLoop {
  4.   public static void main(String[] args) {
  5.     int n;
  6.    
  7.     Scanner input = new Scanner(System.in);
  8.    
  9.     while (true) { // Condition in while loop is always true here
  10.       System.out.println("Input an integer");
  11.       n = input.nextInt();
  12.      
  13.       if (n == 0) {
  14.         break;
  15.       }
  16.       System.out.println("You entered " + n);
  17.     }
  18.   }
  19. }

Java while loop break continue program

  1. import java.util.Scanner;
  2.  
  3. class BreakContinueWhileLoop {
  4.   public static void main(String[] args) {
  5.     int n;
  6.    
  7.     Scanner input = new Scanner(System.in);
  8.    
  9.     while (true) {
  10.       System.out.println("Input an integer");
  11.       n = input.nextInt();
  12.      
  13.       if (n != 0) {
  14.         System.out.println("You entered " + n);
  15.         continue;
  16.       }
  17.       else {
  18.         break;
  19.       }
  20.     }
  21.   }
  22. }

Continue statement takes the control to the beginning of the loop and body of the loop executes again. Whatever you can do with a while loop can be done with a for loop or a do while loop.