Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.14. The do-while statement

3.14. The do-while statement
Last updated: 23 January 2013.

The do-while statement allows you to repeat the execution of the same statements as long as a certain condition evaluates to true. Here is the syntax of the do-while statement:

do {

   
// the statements that are to be executed repeatedly go here

}

while (<condition>);

condition is an expression that evaluates to true or false. The statements are executed at least once since the condition is evaluated at the bottom. In the example shown below, the method doWhileLoop prints the positive integers under 10 to the standard output:

public class JavaLoops {

   
public void doWhileLoop(){

         
final int MAX_VALUE = 10;
         
int i = 0;

         
do{
               
System.out.println(i);
                ++i;
         
}
         
while( i < MAX_VALUE);
   
}

}

Create a class named App and call the method doWhileLoop:

public class App {

   
public static void main(String[] args) {

         
new JavaLoops().doWhileLoop();

   
}

}

The output is:

0
1
2
3
4
5
6
7
8
9


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.14. The do-while statement
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.15. The continue statement  >  

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7