AppDividend
Latest Code Tutorials

Java Do While Example | Do While In Java Tutorial

0

Java Do While Example | Do While In Java Tutorial is today’s topic. We have already seen the while loop in Java in the previous topic. Java do-while loop is used to execute a block of statements continuously until the given condition is false. The do-while loop in Java is similar to while loop except that a condition is checked after all the statements are executed, so the do while loop guarantees a loop execution at least once.

Java Do While Example

Java do-while loop is used to iterate the part of a program several times. If several iterations are not fixed and you must have to execute a loop at least once even if the expression condition is false because that code block is executed before a condition is tested. If that is the scenario then it is recommended to use the do-while loop. See the following syntax.

do{  
//code to be executed  
}while(expression);

The expression for a do-while loop must return the boolean value; otherwise, it will throw the compile-time error. See the following code example.

public class Conditionals {

  public static void main(String[] args) {

    int m = 3;
    do {
      System.out.println(m);
      m++;
    } while (m <= 8);
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
3
4
5
6
7
8
➜  java

#How loop works

First, the statements inside the loop execute, and then the condition gets evaluated. If the condition returns true, then the control gets transferred to the “do” else it jumps to the next statement after do-while. See the following pseudo-code.

The body of the do…while loop is executed once before checking a test expression. Only then, the test expression is checked.

If the test expression condition is evaluated to true, then codes inside the body of a loop are executed, and that test expression is evaluated again. This process goes continue until the test expression is evaluated to false.

When the test expression is false, the do…while loop terminates.

#Difference Between Do while and while loop

The only time you should use do-while loop is when you want to execute the statements inside the loop at least once, even though the condition expression returns false. Otherwise, it’s always better to use the while loop.

Java while loop looks cleaner than the do-while loop.

That’s all for java do-while loop. You should also look into the java for loop and java continue statement.

#Java Infinitive Loop

If you pass true in the expressions parameter in the do-while loop, it will be an infinite do-while loop.

See the following syntax.

do{  
 //code to be executed  
}while(true);

See the following code.

public class Conditionals {

  public static void main(String[] args) {

    int m = 3;
    do {
      System.out.println(m);
      m++;
    } while (true);
  }
}

It will go into the infinite loop, and you need to break it manually.

#Iterating array using a do-while loop

Let’s see an array iterating through a do/while loop. See the following code.

public class Conditionals {

  public static void main(String[] args) {

    int arr[] = { 21, 19, 18, 29 };
    // i starts with 0 as array index starts with 0
    int m = 0;
    do {
      System.out.println(arr[m]);
      m++;
    } while (m < 4);
  }
}

See the output.

➜  java javac Conditionals.java
➜  java java Conditionals
21
19
18
29
➜  java

Finally, Java do/while Example | Do While In Java Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.