Java Continue Statement with Example

A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump. In this tutorial, we will learn about the Java jump statement. The jump statement can be used to transfer the control to other parts of the program. The java continue statement is one of the jump statement.



Java Continue Statement:

The java continue statement skips the current iteration of a for loop, while loop, and do-while loop. We can use a continue statement with all types of loops such as: for loop, while loop, and do-while loop. In the while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In for loop, the control goes first to the iteration portion of the for statement and then to the conditional expression.




Syntax of Java continue statement:

 continue; 


Flowchart of Java continue statement:

Java Continue Statement

Java Continue Statement Example:

class ContinueStatement{
	public static void main(String args[]){
		for(int i = 1; i<=10; i++){
			if(i%2 == 0)
				continue;
			//print odd values.
			System.out.print(i+ " ");
		}
	}
}


Output:
 1 3 5 7 9 


Java continue statement with labeled inner for loop:

As with the break statement, the continue may also specify a label to describe which enclosing loop to continue. Here is an example that demonstrates the use of continue statement with label inside an inner for loop. 


Example of java continue statement with labeled for loop:


class LabelContinue{
	public static void main(String args[]){
		aa:
		for(int i = 1; i<=3; i++){
			bb:
			for(int j = 1; j<=3; j++){
				if(i==2 && j==2){
					continue aa;	
				}
				System.out.println(i+ " " +j);	
			}
			
		}
	}
}


Output:

1  1
1  2
1  3
2  1
3  1
3  2
3  3



You may also like these related posts:
1. Java switch statement with example.

Java Continue Statement with Example Java Continue Statement with Example Reviewed by Prashant Srivastava on December 20, 2019 Rating: 5

No comments:

Powered by Blogger.