Learn About Java While and Do While Loop Statements

Java’s iteration statements are for, while, and do-while. These statements produce what we commonly call loops. As you presumably know, a loop repeatedly executes the same set of instructions until a termination condition is reached. As you will notice, Java has a loop to meet any programming need.

Java While Loop Statement

The while loop is Java’s most fundamental loop statement. It recurs a statement or block, while its predominant or controlling expression is true. Here is its common form:

while(condition) { // body of loop


The condition can be consist of a Boolean expression. The loop body will be executed as long as the conditional expression is true. But when the condition becomes false, control transfers to the next line of code instantly following the loop. The curly braces are avoidable if only a single statement is being repeated.

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

     // Demonstrate the while loop.
     class While {
       public static void main(String args[]) {
         int n = 10;
         while(n > 0) {
           System.out.println("tick " + n);




When you will run this program, it will “tick” ten times:

tick 10
         tick 9
         tick 8
         tick 7
         tick 6
         tick 5
         tick 4
         tick 3
         tick 2
         tick 1

As the while loop assess its conditional expression at the top of the loop, the loop body will not run even once, if the condition is false, to start with. For instance, in the following fragment, the call to println() is never executed:

int a = 10, b = 20;
while(a > b)
  System.out.println("This will not be displayed");

The body of the while (or any Java loops) can be blank. This is for the reason that a null statement (one that includes only of a semicolon) is syntactically correct in Java. For instance, consider the following program:

// The target of a loop can be empty.
class NoBody {
  public static void main(String args[]) {
    int i, j;
    i = 100;
    j = 200;
    // find midpoint between i and j
    while(++i < --j); // no body in this loop
    System.out.println("Midpoint is " + i);

This program identifies the midpoint between i and j. It produces the following output: Midpoint is 150

Here is how this while loop will work.

The value of i is incremented, and the value of j is decremented. These values are then matched with one another. If the new value of i is still smaller than the new value of j, then the loop repeats. If i is equivalent to or greater than j, the loop ends. On exit from the loop, i will sustain a value that is halfway between the original values of i and j. (Of course, this course of action will only work when i is less than j, to begin with.)

As you can see, there is no requirement for a loop body; all of the processes happen within the conditional expression, itself. In professionally typed Java code, short loops are often coded without bodies when the controlling expression can manage all of the details itself.

Java Do While Loop Statement

As you just observed, if the conditional expression checking a while loop is originally false, then the body of the loop will not execute at all. Nevertheless, sometimes it is helpful to run the body of a loop at least once, even if the conditional expression is false, to start with.

In other terms, there are circumstances when you would prefer to test the termination expression at the end of the loop rather than at the starting. Luckily, Java provides a loop that does just that: the do-while. The do-while loop every time runs its body at least once, as its conditional expression is at the bottom of the loop. Its common form is

do {
// body of loop

} while (condition);

Every iteration of the do-while loop first runs the loop body and then evaluates the conditional expression. If the expression is true, the loop will recur or repeat. Otherwise, the loop ends. As with all of Java’s loops, the condition must be a Boolean expression.

Here is a reworked form of the “tick” program that illustrates the do-while loop. It produces the same output as before.

// Demonstrate the do-while loop.
class DoWhile {
  public static void main(String args[]) {
    int n = 10;
    do {
      System.out.println("tick " + n);
    } while(n > 0);


The loop in the previous program, while technically correct, can be rewritten more efficiently as follows:

do {
  System.out.println("tick " + n);
} while(--n > 0);

In this instance, the expression (– –n > 0) combines the decrement of n and the test for zero in one expression. Here is how it works.

First, the – –n statement executes, decrementing n and passing the new value of n. This value is then equaled with zero. If it is more than zero, the loop proceeds; otherwise, it terminates.

The do-while loop is particularly useful when you process a menu selection because you will typically want the body of a menu loop to execute at least once. Examine the following program, which implements a very simple help system for Java’s selection and iteration statements:

// Using a do-while to process a menu selection
class Menu {
  public static void main(String args[])
    throws java.io.IOException {
    char choice;
do {
      System.out.println("Help on: ");
      System.out.println("  1. if");
      System.out.println("  2. switch");
      System.out.println("  3. while");
      System.out.println("  4. do-while");
      System.out.println("  5. for\n");
      System.out.println("Choose one:");
   choice = (char) System.in.read();
    } while( choice < '1' || choice > '5');
    switch(choice) {
      case '1':
        System.out.println("The if:\n");
        System.out.println("if(condition) statement;");
        System.out.println("else statement;");
      case '2':
        System.out.println("The switch:\n");
        System.out.println("switch(expression) {");
        System.out.println("  case constant:");
        System.out.println("    statement sequence");
        System.out.println("    break;");
        System.out.println("  //...");
      case '3':
        System.out.println("The while:\n");
        System.out.println("while(condition) statement;");
      case '4':
        System.out.println("The do-while:\n");
        System.out.println("do {");
        System.out.println("  statement;");
        System.out.println("} while (condition);");
      case '5':
        System.out.println("The for:\n");
        System.out.print("for(init; condition; iteration)");
        System.out.println(" statement;");

Here is a sample run generated by this program:

Help on: 1. if

      2. switch
      3. while
      4. do-while
      5. for

    Choose one:
    The do-while:
    do {

    } while (condition);

In this program, the do-while loop is used to check that the user has entered a valid choice. If not, then the user is prompted. As the menu must be displayed at least once, the do-while is the perfect loop to achieve this.

See also:

Leave a Comment