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

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

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




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