Do-while Loop Syntax
The syntax for the do-while loop construct is:
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:
The output is:
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:
The output is:
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:
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:
The output is the same as for the first program above, that is:
The following program repeats the second program above, but with a while-loop:
The output is the same as for the second program above, that is:
Comparing with for-loop
The syntax for the for-loop is:
//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:
The output is the same as for the first program above, that is:
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:
The output is the same as for the second program above, that is:
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.