Java continue Keyword

The Java continue statement skips the current iteration of a for loop, while loop, or do-while loop and moves to the next iteration. The usage of continue keyword is very similar to break keyword except the latter terminates the loop itself.

The ‘continue‘ statement can be used inside any kind of loop.

  • inside the for-loop, the continue statement causes control to immediately move to the increment/decrement statement;
  • inside the while or do-while loop, control immediately moves to the condition.

In the following example, a sequence of numbers is output. Odd numbers are skipped.

int n = 10;
for (int i = 0; i < n; i++) {
    if (i % 2 != 0) {
        continue;
    }
    System.out.print(i + " ");
}

The output:

0 2 4 6 8

1. Syntax

The syntax of the continue statement is very straightforward. We can use it inside a loop, either with a label or without a label.

while (testExpression) {
  //statement(s)
  
  if(continue-condition)
    continue;

  //statement(s)
}

//statement(s)

In the above example, when the continue-condition evaluates to true then continue statement terminates the current iteration and control is passed to the testExpression in the beginning of the while loop.

2. Type of continue Statements

There are two forms of the continue statements:

The unlabeled continue statement skips to the end of the innermost loop’s body and evaluates the condition expression that controls the loop. In more general terms, continue skips the statements after the continue statement and keeps looping.

for (initialization; condition; update) {
  
  //statement(s)
  for (initialization; condition; update) {
    //statement(s)
    
    if(continue-condition)
      continue;

    //statement(s)
  }
}

On the other hand, a labeled continue statement skips the current iteration of a loop marked with the given label. In the following example, the control is moved to the outer loop when the continue statement is executed.

label outer_loop;
for (initialization; condition; update) {
  
  //statement(s)
  label inner_loop;
  for (initialization; condition; update) {
    //statement(s)
    
    if(continue-condition)
      continue inner_loop;

    //statement(s)
  }
}

Similar to the break statement, the continue statement only affects the loop in which it is located. The continue statement cannot skip the current iteration of the outer loop.

3. Continue Statement Example

Let’s look at one example to understand better continue statements in Java. The program uses a for loop to iterate over numbers from 0 to 9.

  • If the number is even, the iteration is skipped using the continue statement.
  • If the number is odd, it is printed in the console.
for( int i = 0 ; i < 10 ; i++ ) {

  if( i % 2 == 0) {
    continue;    //if i is even, skip the current iteration
  }

  System.out.println("The number is " + i );
}

The program output is:

The number is 1
The number is 3
The number is 5
The number is 7
The number is 9

Often, we can rewrite our loop without using the continue statement. Here is an example:

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

  if( i % 2 == 0) {
    System.out.println("The number is " + i );
  }
}

It prints the same output as the previous program with the ‘continue‘ statement.

4. Conclusion

The continue statement is used to skip the current iteration of a loop and proceed to the next one. It is useful for situations where you want to skip specific iterations based on certain conditions.

However, it’s essential to exercise caution when using it. Overuse of break and continue statements can lead to code that is challenging to read and maintain. 

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode