Introduction to Do While Loop
The difference between do-while and while loop is that do-while evaluates its boolean-expression at the bottom of the loop instead of the top. Therefore, the block of statements within the do block are always executed at least once.
Syntax
do {
statement(s)
} while (boolean-expression);
The do-while always executes at least once, even if the boolean-expression evaluates to false the first time.
Example
1. /**
2. * @(#)DoWhileLoopDemo.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/11
7. */
8.
9. public class DoWhileLoopDemo {
10.
11. /**
12. * Creates a new instance of DoWhileLoopDemo.
13. */
14. public DoWhileLoopDemo() {
15. }
16.
17. /**
18. * @param args the command line arguments
19. */
20. public static void main(String[] args) {
21. // TODO code application logic here
22.
23. int count = 1;
24.
25. do {
26. System.out.println("Count is: " + count);
27. count++;
28.
29. } while (count < 6);
30. }
31. }
Output
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
