Do-while loop in Java

Do-while loop is slightly different from the while loop:

In case of while loop, if the conditional expression controlling a while loop is false then the loop won’t execute even for once. But sometimes it is desirable that we enter the loop at least once.

  • It can certainly be done using while loop by alterling the condition a bit, but it may not seem natural.
  • do-while loop is a modified form of while loop made for this purpose only.

Syntax

Its general form is :

  • First the loop body is executed once.
  • Then the condition is checked,
    • if it is true, then the loop body is executed again, condition is checked and so on.
    • If it is false the statements after the do-while loop are executed.
  • Condition is any boolean expression meaning it should evaluate to only true or false and nothing else.
  • Note the semi-colon after while(condition), it is necessary.

Control Flow Diagram of the do-while loop

The Control Flow diagram for a do-while loop looks like:

do while loop
do while loop

An Example

For example the code below computes the sum of first 100 integers and prints them :

The output of the code is :

Let’s see how this works (It is as same as the example from the while loop):

  • First the value of sum and i, both are assigned to 0.
  • After 1st iteration: sum = sum + i = 0 + 0 = 0, i = i+1 = 0+1 = 1
  • After 2nd iteration: sum = sum + i = 0 + 1 = 1, i = i+1 = 1+1 = 2
  • After 3rd iteration: sum = sum + i = 1 + 2 = 3, i = i+1 = 2+1 = 3
  • After 4th iteration: sum = sum + i = 3 + 3 = 6, i = i+1 = 3+1 = 4
  • After 5th iteration: sum = sum + i = 6 + 4 = 10, i = i+1 = 4+1 = 5
  • .
  • .
  • After 100th iteration: sum = sum + i = 4950 + 100 = 5050, i = i+1 = 100+1 = 101
  • Since i = 101, the condition i<=100 becomes false and loop breaks. The next line of the code is a print statement which prints the value of the computed sum.

Typical use of do-while loop

In the example below, no matter what the intial conditions were control enters the loop body at least once. It demonstrates a binary switch which can be either on or off:

The output is :

Note a few things here:

  • System.in.read() reads input from the keyboard, character by character and returns them as integers. We cast them again to characters. If the character entered is anything other than 0/1, it repeats itself. Whenever 0/1 is entered loop, terminates. If loop terminates on 0, Off! is printed and On! is printed in case the loop terminates on 1.
  • throws java.io.IOException was needed  to handle input errors because it uses System.in.read().
  • import java.io.IOException is used to import IOException class to be used. You will learn more about it after you learn about classes and exception handling.
  • When you press ctrl + C  the code terminates.

Empty do-while loop

Loop body can be left empty, if the condition inside while can handle the required computation:

The output is :

 


Infinite do-while loop

Like while loop, you can also make an infinite do-while loop. The code below only filters the 0 and 1 from an infinite string of characters. It prepares a binary string by adding the filtered 0/1 to the end of the same string.

The output is :

 


Processing array with a do-while loop

Code below checks if the array elements are even or odd and prints them accordingly.

The output is :

array.length is a variable defined inside array class. It holds the value of the length of the array. As long as the variable i is smaller than length of the array, the loop repeates itself. When i becomes as large as the length of the array, the condition i<array.length becomes false and the loop terminates.

Note: Be careful when processing an array with a do-while loop. The array must contain at least one element because the the do part executes first and the condition on emptiness is checked later.


Nesting do-while loops

If one do-while loop contains another do-while loop inside its body then the do-while loops are said to be nested. This nesting can go on upto any level. The code below shows two-level nesting i.e two do-while loops are nested one-inside another:

The output is :

Note that you could have easily replaced any of the do-while loops with some other loop, say for-loop or while loop.


Use of break in do-while loop

There are occasions when you no longer want to iterate over all the elements because some condition has already been met. In those cases, you have to terminate the loop prematurely. break is a keyword used specifically for this purpose.

The output is :

break, takes you out of the loop by one level. Had there been another loop surrounding the do-while loop in the previous example, internal loop would have been broken but the control would have still been surrounded by the external loop. Depending on the need, you might have used another break.


Use of continue in a do-while loop

Sometimes you want to skip rest of the instructions in a loop but you still want to continue running the loop, continue is a statement made specifically for this purpose. However, there is a subtle difference in control transfer as compared to the while loop.

The output is :

In case when i is divisible by 2, the continue statement sends the control to the end of the loop where the condition is checked. If the condition is true then the control is transferred to the beginning of the loop and then the statements in the do-while loop execute again from the start.


Labelled do-while loop

You can label a do-while loop using any valid Java token followed by a colon. Like in:

Labelling has many uses. For example,  break with label can be used to break out of a series of nested loops to the end of a labelled loop:

The output is: