Do-while loop in java

Let’s learn do-while loop in java.

Do-while loop in java

Do while loop java executes the statements and then evaluates the expression/condition of loop’s body until given condition is true.

Also read – for loop java

So do while loop is guaranteed to execute the body at least once. Here expression in do while loop should return a boolean value. Here’s syntax of do while loop in java.

// do while loop java
do
{
   statement(s);
}while(expression/condition);



Flow diagram of do while loop in java

do while loop in java

In the above flow diagram first statements inside loop execute then condition gets evaluated.

Also read – java while loop break

If condition is true then control gets shifted inside “do”. Else, jumps outside loop.

Here’s do-while loop in java example.

public class DoWhileDemo
{
   public static void main(String[] args)
   {
      int x = 1;
      do
      {
         System.out.println(x);
         x++;
      }
      while(x <= 5);
   }
}


Output:

1
2
3
4
5


do while Infinite loop

An infinite do while loop is a loop that will execute endlessly because loop’s expression is always true. Here’s do while infinite loop example.

public class InfiniteDoWhileDemo
{
   public static void main(String[] args) throws InterruptedException
   {
      do
      { 
         System.out.println("Start do while");
         System.out.println("End do while");
         Thread.sleep(3 * 1000);
      }while(true);  // press ctrl+c to exit 
   }
}



Similarly we can iterate an array using do-while loop. Let’s see an example on iterating an array using do-while loop.

class DoWhileLoopArray 
{
   public static void main(String args[])
   {
      int[] arrNum = {2, 4, 6, 8};
      int a = 0;
      do
      {
         System.out.println(arrNum[a]);
         a++;
      }while(a < 4);
   }
}


Output:

2
4
6
8


java do while loop with user input

Now let’s learn to add user input numbers and get total using do while loop. In the below java program if user inputs 0, do while loop terminates the loop.

Here’s the java do while loop with user input.

import java.util.Scanner;
public class DoWhileUserInput
{
   public static void main(String[] args) 
   {
      int number, total = 0;
      Scanner sc = new Scanner(System.in);
      do{
          System.out.println("Please enter number: ");
          number = sc.nextInt();
          total += number;
      }while (number != 0);  
      System.out.println("Total is = " + total);
      sc.close();
   }
}


Output:

Please enter a number: 5
Please enter a number: 1
Please enter a number: 9
Please enter a number: 4
Please enter a number: 2
Please enter a number: 9
Please enter a number: 4
Please enter a number: 0
Total is = 34


Write a program to display number 1 to 50 using do while loop in java

Let’s learn to write a program to display number 1 to 50 using do while loop in java.

import java.util.Scanner;
public class ProgramDisplayNumber 
{
   public static void main(String[] args) 
   {
      int counter;
      int number;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter maximum number: ");
      number = sc.nextInt();
      counter = 1;
      do{
          System.out.println(counter + " ");
          counter++;
      }while(counter <= number);
      sc.close();
   }
}


Output:

Please enter maximum number: 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50


difference between while and do-while loop in java

while loopdo while loop
while loop will check condition before the loop starts.do while loop will check condition after the loop starts.
while loop will run zero times.do while loop will run at least once.
while loop executes code only if the condition is true.do while loop executes statements at least once even if condition fails.

Let’s see an example on difference between while and do-while loop in java.

// while loop runs zero times
int a = 20;
while(a < 5)  // here condition executes false
{
   System.out.println("hello world");
   a++;
}

---------------------------------------------------------

// do-while loop
int a = 20;
do
{
   System.out.println("hello world");
   a++;
}while(a < 5);  // here condition executes false