Do-While Loop in Java


JavaViews 2040

Do-While Loop in Java is another type of loop control statement. Similar to while loop which we learned in the previous tutorial, the do-while loop also executes a block of code based on the condition. The only difference is that Do-While Loop in Java executes the code block at least once since it checks the condition at the end of the loop.

Do-While Loop in Java Syntax

do {
  //code
  //update counter
}while(condition);

code – block of statements inside the java do-while loop

update counter – updating the value of the variable in the condition.

condition – the test expression or condition to validate for the java do-while loop to execute.

do-while loop flowchart

java do while loop

  • When the execution control points to the do statement, it executes the code inside the loop first.
  • It then updates the counter variable value.
  • At the end, when the control points to the while statement, it evaluates the condition. Since it checks the condition towards the end, the code inside the java do-while loop is executed at least once.
  • If the condition is true, it again proceeds with the loop execution until the condition returns false.
  • When the condition is false, it exits the java do-while loop. Hence it is also called an exit control loop.

Simple do-while loop example

public class simpleDowhileloop {

  public static void main(String[] args) {
    int i=0;
    do {
      System.out.println("i: " + i);
      i++;
    }while(i<=5);

  }

}
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5

This is a simple example to demonstrate the working of a java do-while loop.

First, we declare an integer variable with value 0 (i=0).

Next, when the control points to the do statement, it starts the execution of the statements inside the do-while loop. It prints the value i:0 and increments i value by 1.

Now, i=1. Then it checks the condition i<=5 since this condition is true, it continues the execution of the do-while loop.

It repeats this process until i=5.

Now after it prints i:5value increments to i=6. At this stage, the condition i<=5 returns false and stops the execution of the do-while loop.

Do-While Loop in Java with an array

We can use java do-while loop to retrieve array elements similar to a while loop. Below is an example to retrieve individual array elements and then print the sum of all array elements.

public class doWhileLoopArray {

  public static void main(String[] args) {
    int[] numbers = {40,10,20,50,30};
    int i=0,sum=0;
    do {
      System.out.println("Array element : " + numbers[i]);
      sum = sum + numbers[i];
      i++;
    }while(i<numbers.length);
    System.out.println("Sum of array elements: " + sum);

  }

}
Array element : 40
Array element : 10
Array element : 20
Array element : 50
Array element : 30
Sum of array elements: 150

First, we declare an array of integers variable numbers, counter variable i  and variable sum to store the sum value. Inside do loop, we can access the individual array elements using the counter variable. Since i=0, it retrieves the first element numbers[0] and stores in the sum variable. Next, increments value and now i=1. Finally, checks the condition in the while statement if i<array length. Here array length is 5. Hence this condition is true and continues the execution of do loop. It continues this step until i=6 and when the condition returns false, exits the do-while loop.

Infinite Do-While Loop in Java

Similar to while loop, we can also have an infinite do-while loop when we do not use the right condition or do not update the counter variable properly.

In the below example, it prints the statement infinitely until the user terminates the program. This is because the condition always returns a true value.

public class infiniteDowhileloop {

  public static void main(String[] args) {
    int i = 0;
    do {
      System.out.println("Infinite loop");
      i++;
    }while(i>=0);

  }

}

In the below example, it prints the statement infinitely since we are not updating the variable value anywhere within the loop. Hence the condition always returns a true value.

public class infiniteDowhileloop {

  public static void main(String[] args) {
    Boolean bFlag = true;
    do {
      System.out.println("Infinite loop");
    }while(bFlag);

  }

}

Difference between For, While and Do-While Loop

Now that we have learned about different loop statements, let us see the major differences between for, while and do-while loops.

For loopwhile loopdo-while loop
we need to initialize the loop variable in for loopwe can initialize the loop variable before while loopwe can initialize the loop variable before do-while loop
scope of loop variable is within the loopscope of loop variable is not limited inside the loop and can be used outside as well.scope of loop variable is not limited inside the loop and can be used outside as well.
Checks the condition at the beginning of the loopChecks the condition at the beginning of the loopChecks the condition towards the end of the loop
For loop might not execute even once if the condition is false at the start of execution itselfFor loop might not execute even once if the condition is false at the start of execution itselfExecutes the statements atleast once since we check the condition at the end
for(initialization;condition,updatecounter) {
//code
}
while(condition) {
//code
//update counter
}
do {
//code
//update counter
}while(condition);

Reference

Translate »