Home Java Tutorial Java continue Statement with examples

Java continue Statement with examples

by Anup Maurya
36 minutes read

In this tutorial, you will learn about the continue statement and labeled continue statement in Java with the help of examples.

In Java, the continue statement is used to skip over the remaining statements in a loop and immediately start the next iteration of the loop. It allows you to control the flow of execution in a loop and skip over certain iterations based on some condition. In this tutorial, we’ll discuss the continue statement in detail with some examples.

Syntax of the continue statement

The continue statement has the following syntax:

continue;

The continue statement is used inside loops to skip over the remaining statements in the loop and immediately start the next iteration of the loop.

Example: Using the continue statement inside a loop

The continue statement is often used inside loops to skip over certain iterations based on some condition. Here’s an example that uses a for loop to print out the odd numbers from 1 to 10:

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        continue;
    }
    System.out.println(i);
}

In this example, the loop iterates from 1 to 10, and the if statement checks whether i is even. If i is even, the continue statement is executed, which skips over the remaining statements in the loop and immediately starts the next iteration. If i is odd, the println() statement is executed, which prints out the value of i. The output of this program would be:

1
3
5
7
9

Example: Using the continue statement inside a nested loop

The continue statement can also be used inside nested loops to skip over certain iterations of the inner loop based on some condition. Here’s an example that uses a nested for loop to print out the multiplication table from 1 to 10, but skips over the even multiples:

for (int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 10; j++) {
        int product = i * j;
        if (product % 2 == 0) {
            continue;
        }
        System.out.print(product + "\t");
    }
    System.out.println();
}

In this example, the outer loop iterates from 1 to 10, and the inner loop iterates from 1 to 10 as well. The if statement inside the inner loop checks whether the product of i and j is even. If the product is even, the continue statement is executed, which skips over the remaining statements in the inner loop and immediately starts the next iteration of the inner loop. If the product is odd, the print() statement is executed, which prints out the product followed by a tab character. The output of this program would be:

1	3	5	7	9	
3	9	15	21	27	
5	15	25	35	45	
7	21	35	49	63	
9	27	45	63	81	
11	33	55	77	99	
13	39	65	91	117	
15	45	75	105	135	
17	51	85	119	153	
19	57	95	133	171	

Conclusion

In this tutorial, we discussed the continue statement in Java, which is used to skip over the remaining statements in a loop and immediately start the next iteration of the loop. We looked at some examples of how to use the continue statement inside a loop and a nested loop. By using the continue statement, you can control the flow of execution in your loops and skip over certain iterations based on some condition. It’s a powerful tool that can be used to simplify your code and make it more efficient. However, be careful when using the continue statement, as it can sometimes make your code more difficult to understand and debug.

Some best practices for using the continue statement in Java include:

  • Use the continue statement sparingly, and only when it simplifies your code and makes it more efficient.
  • Be careful when using the continue statement inside nested loops, as it can sometimes make your code more difficult to understand and debug.
  • Use comments to explain why you’re using the continue statement, and what it does.

By following these best practices, you can use the continue statement effectively in your Java code and make it more readable, maintainable, and efficient.

related posts

Leave a Comment

Enable Notifications OK No thanks