Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.13. The while statement

3.13. The while statement
Last updated: 23 January 2013.

The 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 while statement:

while (<condition>) {

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

}

condition is an expression that evaluates to true or false. In the example shown below, the method whileLoop prints the positive integers under 10 to the standard output:

public class JavaLoops {

   
public void whileLoop(){

         
final int MAX_VALUE = 10;
         
int i = 0;

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

}

Create a class named App and call the method whileLoop:

public class App {

   
public static void main(String[] args) {

         
new JavaLoops().whileLoop();

   
}

}

The output is:

0
1
2
3
4
5
6
7
8
9


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.13. The while statement
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.14. The do-while 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