Java while Loop

In this chapter you will learn:

  1. How to use the basic Java While Loop
  2. How to use do-while statement
  3. How to create a simple help menu with while loop and switch statement

Java While Loop Syntax

The while loop repeats a statement or block while its controlling condition is true. Here is its general form:

while(condition) { 
   // body of loop 
}

Please note:

  • The condition can be any Boolean expression.
  • The body of the loop will be executed as long as the conditional condition is true.
  • The curly braces are unnecessary if only a single statement is being repeated.

Here is a while loop that counts down from 10, printing exactly ten lines of "tick":

public class Main {
  public static void main(String args[]) {
    int n = 10;//  j a  va  2 s . c o  m
    while (n > 0) {
      System.out.println("n:" + n);
      n--;
    }
  }
}

When you run this program, you will get the following result:

The body of the while loop will not execute if the condition is false. For example, in the following fragment, the call to println() is never executed:

public class Main {
  public static void main(String[] argv) {
    int a = 10, b = 20;
    while (a > b) {
      System.out.println("This will not be displayed");
    }// j av a  2 s  .com
    System.out.println("You are here");
  }
}

The output:

The body of the while can be empty. For example, consider the following program:

public class Main {
  public static void main(String args[]) {
    int i, j;/*  j  a  va 2s  .c o  m*/
    i = 10;
    j = 20;

    // find midpoint between i and j
    while (++i < --j)
      ;
    System.out.println("Midpoint is " + i);
  }
}

The while loop in the code above has no loop body and i and j are calculated in the while loop condition statement. It generates the following output:

do-while statement

To execute the body of a while loop at least once, you can use the do-while loop. Its general form is:

do { 
   // body of loop 
} while (condition);

Here is an example to show how to use a do-while loop.

public class Main {
  public static void main(String args[]) {
    int n = 10;//from ja  v  a2 s.  co m
    do {
      System.out.println("n:" + n);
      n--;
    } while (n > 0);
  }
}

The output:

The loop in the preceding program can be written as follows:

public class Main {
  public static void main(String args[]) {
    int n = 10;/* ja  v  a 2s .co m*/
    do {
      System.out.println("n:" + n);
    } while (--n > 0);
  }
}

The output is identical the result above:

Help menu with While and Switch

The following program implements a very simple help system with do-while loop and switch statement.

public class Main {
  public static void main(String args[]) throws java.io.IOException {
    char choice;//from  j  av a  2 s  .co  m
    do {
      System.out.println("Help on:");
      System.out.println(" 1. A");
      System.out.println(" 2. B");
      System.out.println(" 3. C");
      System.out.println(" 4. D");
      System.out.println(" 5. E");
      System.out.println("Choose one:");
      choice = (char) System.in.read();
    } while (choice < '1' || choice > '5');
    System.out.println("\n");
    switch (choice) {
      case '1':
        System.out.println("A");
        break;
      case '2':
        System.out.println("B");
        break;
      case '3':
        System.out.println("C");
        break;
      case '4':
        System.out.println("D");
        break;
      case '5':
        System.out.println("E");
        break;
    }
  }
}

Here is a sample run produced by this program:

Next chapter...

What you will learn in the next chapter:

  1. How to write for loop
  2. How to use variables inside the for loop
  3. How to use comma in the for loop statement
  4. How to write different forms of for loop
  5. How to write nested for loops
Home » Java Tutorial » Operators Statements
Your first Java program
Your second Java program
Java Keywords and Identifiers
Variable stores value in a Java program
Variable Scope and Lifetime
Java Operators
Java Arithmetic Operators
Java Bitwise Operators
Java Relational Operators
Java Boolean Logical Operators
Java If Statement
Java switch Statement
Java while Loop
Java for loop
Java for each loop
Java break statement
Java continue statement
Java return statement
Java Comments
Java documentation comment(Javadoc)