Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.15. The continue statement

3.15. The continue statement
Last updated: 27 January 2013.

The continue statement is used with the for, for-each, while or do-while loop statement. The continue statement jumps to the next iteration of the most closely enclosing loop statement. In the following example, the method printOddNumbers prints the positive odd integers under 10 to the standard output:

public class JavaLoops {

   
public void printOddNumbers(){

         
final int MAX_VALUE = 10;

         
for(int i=0; i<MAX_VALUE; ++i){

               
if(i % 2 == 0)
                     
continue; // if i is an even number, jump to the next iteration

               
System.out.println(i);
         
}
    }

}

Create a class named App and call the method printOddNumbers:

public class App {

   
public static void main(String[] args) {

         
new JavaLoops().printOddNumbers();

   
}

}

The output is:

1
3
5
7
9


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.15. The continue statement
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.16. The break statement

Copyright © 2013 - 2016. 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