Over 500 Magazines for FREE!

Yes! You can subscribe to ALL of them. They will be shipped to your home FREE of cost!
Kindly click here to apply!

Continue Statement

Continue statement is used when we want to skip the rest of the statement in the body of the loop and continue with the next iteration of the loop.
There are two forms of continue statement in Java.

1. Unlabeled Continue Statement
2. Labeled Continue Statement

Unlabeled Continue Statement

This form of statement causes skips the current iteration of innermost for, while or do while loop.

For example,

  1. for(int var1 =0; var1 < 5 ; var1++)
  2. {
  3.  
  4. for(int var2=0 ; var2 < 5 ; var2++)
  5. {
  6. if(var2 == 2)
  7. continue;
  8.  
  9. System.out.println(“var1:+ var1 + “, var2:+ var2);
  10.  
  11. }
  12.  
  13. }

In above example, when var2 becomes 2, the rest of the inner for loop body will be skipped.

Labeled Continue Statement

Labeled continue statement skips the current iteration of the loop marked with the specified label. This form is used with nested loops.

For example,

  1. Outer:
  2. for(int var1 =0; var1 < 5 ; var1++)
  3. {
  4.  
  5. for(int var2=0 ; var2 < 5 ; var2++)
  6. {
  7. if(var2 == 2)
  8. continue Outer;
  9.  
  10. System.out.println(“var1:+ var1 + “, var2:+ var2);
  11.  
  12. }
  13.  
  14. }

In the above example, when var2 becomes 2, rest of the statements in body of inner as well outer for loop will be skipped, and next iteration of the Outer loop will be executed.

Continue Statement Examples

Post new comment

To combat spam, please enter the code in the image.




Do you have a better example?
We're sure you have hundreds of Java program examples.

Spare some time and submit your java example here even if you think it's too small to contribute.

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!