Java Continue Statement With Examples

Continue statement in Java is used to force the iteration of the loop. As soon as a continue statement is encountered in a loop, remaining statements in the loop that follow the continue statement are not executed and the control jumps to the next iteration of the loop.

What happens after the control jumps to the beginning of the loop differs based on the type of loop used.

  1. For while loop and do-while loop, continue statement causes the transfer of control to the condition of the loop.
  2. In case of for loop, continue statement causes the transfer of control initially to the increment part of the for loop and then to the condition that controls the loop.

Java continue statement examples

1- Using continue statement in a for loop to print only odd numbers between 1-10.

Output

2- Here is another example using continue statement with while loop. In the example user is prompted to enter a number until a positive integer is entered. When a positive integer is entered control breaks out of the loop.

Output

Labelled continue statement in Java

Just like break statement, continue statement can also be labelled to specify which specific loop has to be iterated.
For labeling a loop you just put a label (any name) in the starting of the loop followed by a colon. To continue iterating that labelled loop you will use the following statement.

Java labelled continue statement example

Output

That’s all for the topic Java Continue Statement With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.