Continue Java Statement With Examples

Continue Java Statement: Welcome to Another new post for the core Java tutorial; in the last post, we discussed the break statement in Java, and in this post, we are going to learn about the continue statement and how to use the continue statement in your project with some simple example for better understanding. The continue statement is mainly used to skip the current iteration of a loop.

What Is Continue Java?

In Java, the continue statement is a flow control statement that allows the programmer to skip executing certain statements in a loop and continue with the next iteration. It is particularly useful when certain iterations of a loop need to be skipped based on certain conditions.

When the continue statement is encountered within a loop, it immediately jumps to the next iteration, skipping any statements that come after it in the current iteration. This can help to streamline code execution and make loops more efficient.

In this article, we will explore the various uses of the continue statement in Java and examine how it can be used to write more efficient and effective code. We will also look at examples of how the continue statement can solve real-world programming problems.

Continue Java Syntax

The continue statement is a Java control statement used to skip the execution of certain statements within a loop and continue with the next iteration. The syntax of the continue statement in Java is as follows:

continue;

This statement is placed within the body of a loop, and when it is executed, the current iteration of the loop is immediately terminated. Control then passes to the next iteration of the loop, skipping any remaining statements in the current iteration.

Labelled Continue Statement

Whatever continue statements we have discussed until now are all unlabelled continue statements. There is another form of continue statement that’s called labelled continue statement. That can skip executing statements that lie inside an outer loop.

package com.softwaretestingo.ConditionalStatements;
public class LabelledContinueStatementEx 
{
	public static void main(String[] args) 
	{
		label:
			for (int i = 1; i < 6; ++i) 
			{
				for (int j = 1; j < 5; ++j) 
				{
					if (i == 3 || j == 2)
						continue label;
					System.out.println("i = " + i + "; j = " + j); 
				}
			} 
	}
}

Output:

i = 1; j = 1
i = 2; j = 1
i = 4; j = 1
i = 5; j = 1

Note: The use of labeled continue is discouraged because it’s tough to understand the execution flow by using this. So, from our point of view, if there is a situation where you need to implement the labeled continue statement, try to refactor your code and try to solve it in a different way to make it more readable. If you are using a labelled continue statement, then that will create an ambiguous situation.

Java Continue Statement Examples

Program: Here is an example of how to use the Java continue statement.
package com.softwaretestingo.ConditionalStatements;
public class Continue_Example 
{
	public static void main(String[] args) 
	{
		for (int i = 1; i <= 10; ++i) 
		{      
			if (i > 4 && i < 9) 
			{
				continue;
			}      
			System.out.println(i);
		}   
	}
}

Output:

1
2
3
4
9
10

Continue Java Important Points

Here below are some important points about the continue statement:

  • We can replace the continue statement with an if-else condition in simple cases. If there are multiple if-else conditions at that time, it’s better to use a continue statement to make your codes more readable.

Ref: article

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment