Java

do-while Loop in Java

A loop is a compound statement whose body can be executed repeatedly. The body of the loop has a number of statements. A loop needs an initial state or initial statement, from which the loop will execute the first time before repeating. Repeating means all the statements in the loop are re-executed, in order, again and again. In order for the loop to repeat after the first pass or any pass, there has to be a statement that will cause it to repeat. In order for a loop to stop repeating, there has to be a condition that will cause the loop not to repeat.

Do-while Loop Syntax

The syntax for the do-while loop construct is:

    //initial statement here
    do {
        //statements
        //cause for next iteration
    } while (condition);

This construct should be read as follows: Considering the initial statement, do all the statements in the loop while the condition allows it. The initial statement ends with a semicolon. The do-compound statement itself also ends with a semicolon. Note that “while” here is a reserved word.

There are three main loops in Java: the do-while loop, the while-loop, and the for-loop. This tutorial explains the do-while loop and compares it to the while-loop and the for-loop.

Article Content

do-while Loop

Using the above syntax, an example of a do-while loop is in the following program:

    class TheClass {
        public static void main(String[] args) {  

            int myInt = 0;
            do {
System.out.print(myInt + " ");
myInt++;
            } while (myInt< 5);

System.out.println();
        }  
    }

The output is:

0 1 2 3 4

The complete construct begins with “int myInt = 0;” and ends at “while (myInt < 5);”. There are two simple statements in the braces. The first statement in the braces prints out the value of the integer, myInt. The second statement increments myInt, adding 1 to it. The condition is “while (myInt < 5)”. So, while myInt is less than 5, the compound statement is re-executed.

This loop has just one main simple statement: to print the value of myInt. The second simple statement is to cause the next iteration. The curly brackets can have more than one main simple statement. The following do-while loop has two main simple statements. The first one adds 2 to myInt, and the second one prints the result of the addition:

    class TheClass {

        public static void main(String[] args) {  

            int myInt = 0;
            do {
myInt = myInt + 2;
System.out.print(myInt + " ");
myInt++;
            } while (myInt< 13);

System.out.println();
        }  
    }

The output is:

2 5 8 11 14

This output needs explanation. First of all, note that the while condition has been changed to “while (myInt < 13)”.

When myInt is 0, 2 is added to it, and myInt becomes 2. Two are printed. The increment adds 1 to myInt, and it becomes 3 at the beginning of the next pass. In the next iteration (pass), myInt is 3. Two is added to it again, and it becomes 5. The increment adds 1 to myInt, and it becomes 6. In the next iteration, myInt is 6. 2 is added to it again, and it becomes 8. The increment adds 1 to myInt, and it becomes 9. In the next iteration, myInt is 9. 2 is added to it again, and it becomes 11. The increment adds 1 to myInt, and it becomes 12. In the next iteration, myInt is 12. 2 is added to it again, and it becomes 14. The increment adds 1 to myint, and it becomes 15. After each iteration, the while condition is checked. At this point, while the condition is checked, the myInt is 15, above 13, after 14 has been printed. The condition results in false, and the repetition of the block, stop.

Comparing with while-loop

The syntax for the while-loop is:

    //initial statement here
    while (condition) {
        //statements
        //cause for next iteration
    }

The main difference between the do-while loop and the while-loop is that for the while-loop, the condition is checked first before the block is executed. Note that the while-loop construct does not end with a semicolon.

The following program repeats the first program above, but with a while-loop:

    class TheClass {

        public static void main(String[] args) {  

            int myInt = 0;
            while (myInt< 5) {
System.out.print(myInt + " ");
myInt++;
            }

System.out.println();
        }  
    }

The output is the same as for the first program above, that is:

0 1 2 3 4

The following program repeats the second program above, but with a while-loop:

    class TheClass {

        public static void main(String[] args) {  

            int myInt = 0;
            while (myInt< 13) {
myInt = myInt + 2;
System.out.print(myInt + " ");
myInt++;
            }

System.out.println();
        }  
    }

The output is the same as for the second program above, that is:

2 5 8 11 14

Comparing with for-loop

The syntax for the for-loop is:

for (initial-state; while-condition; increment) {

//statements

}

Though the more restrictive form, the for-loop is concise of the do-while loop or while-loop. The for-loop has parentheses and a block. The initial statement has been removed from outside and above the construct into the parentheses. The while-condition is the second statement in the parentheses. The cause-for-next-iteration (increment) is the last (third) statement in the parentheses.

Note that the for-loop construct does not end with a semicolon. The following program repeats the first program above, but with a for-loop:

    class TheClass {

        public static void main(String[] args) {  

            for (int myInt = 0; myInt< 5; myInt++) {
System.out.print(myInt + " ");
            }

System.out.println();
        }  
    }

The output is the same as for the first program above, that is:

0 1 2 3 4

There is no semicolon after the increment statement (last statement) in the parentheses.

The following program repeats the second program above, but with a for-loop:

    class TheClass {

        public static void main(String[] args) {  

            for (int myInt = 0; myInt< 13; myInt++) {
myInt = myInt + 2;
System.out.print(myInt + " ");
            }

System.out.println();
        }  
    }

The output is the same as for the second program above, that is:

2 5 8 11 14

Conclusion

The do-while loop in Java repeats the execution of its block, as long as a condition is true. Before the block, the do-while loop needs an initial statement (state). The do-while loop needs a cause-for-next-iteration (increment) statement, usually towards the end of its block. The main difference between the do-while loop and the while-loop is that with the do-while loop, the block is always executed before the condition is checked, while with the while-loop, the condition is always checked before the block is executed. Both the do-while and the while-loop do essentially the same thing. The for-loop is a concise construct for the do-while loop or while-loop.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.