continue statement in Java
-
Last Updated: November 2, 2023
-
By: javahandson
-
Series
In this article, we will learn what is a continue statement in Java and how to use it with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.
continue statement is used inside a loop to stop the execution of the subsequent statements of the loop and move the control to the next iteration of the loop.
In the next sections, we will see some examples to understand the continue statement in more depth.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=1; i<10; i++) { if (i==4 || i==6 || i==8) { continue; } System.out.println("Iteration number : "+i); } } } Output: Iteration number : 1 Iteration number : 2 Iteration number : 3 Iteration number : 5 Iteration number : 7 Iteration number : 9
In the above program, we have written a for loop where i should iterate from 1 to 10.
If the value of i is 4 6 or 8 then we are executing the continue statement which means the sysout statement inside the for loop will be skipped and the control will be passed to the next iteration. This is the reason the outputs 4, 6, and 8 will not be printed.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { for (int i=1; i<=5; i++) { for (int j=1; j<=5; j++) { if (i==3 || i==4) { System.out.println("Inside if condition, value of i : "+i+ " and value of j : "+j); continue; } System.out.println("Inner for loop outside if condition, value of i : "+i+ " and value of j : "+j); } } System.out.println("Control is outside of the outer for loop"); } } Output: Inner for loop outside if condition, value of i : 1 and value of j : 1 Inner for loop outside if condition, value of i : 1 and value of j : 2 Inner for loop outside if condition, value of i : 1 and value of j : 3 Inner for loop outside if condition, value of i : 1 and value of j : 4 Inner for loop outside if condition, value of i : 1 and value of j : 5 Inner for loop outside if condition, value of i : 2 and value of j : 1 Inner for loop outside if condition, value of i : 2 and value of j : 2 Inner for loop outside if condition, value of i : 2 and value of j : 3 Inner for loop outside if condition, value of i : 2 and value of j : 4 Inner for loop outside if condition, value of i : 2 and value of j : 5 Inside if condition, value of i : 3 and value of j : 1 Inside if condition, value of i : 3 and value of j : 2 Inside if condition, value of i : 3 and value of j : 3 Inside if condition, value of i : 3 and value of j : 4 Inside if condition, value of i : 3 and value of j : 5 Inside if condition, value of i : 4 and value of j : 1 Inside if condition, value of i : 4 and value of j : 2 Inside if condition, value of i : 4 and value of j : 3 Inside if condition, value of i : 4 and value of j : 4 Inside if condition, value of i : 4 and value of j : 5 Inner for loop outside if condition, value of i : 5 and value of j : 1 Inner for loop outside if condition, value of i : 5 and value of j : 2 Inner for loop outside if condition, value of i : 5 and value of j : 3 Inner for loop outside if condition, value of i : 5 and value of j : 4 Inner for loop outside if condition, value of i : 5 and value of j : 5 Control is outside of the outer for loop
In the above program, we have written a for loop where i should iterate from 1 to 5.
We have also written an Inner for loop that will iterate 5 times for each iteration of i.
But we have also added an if condition which says if i == 3 or i == 4 then we should skip the next statements of the inner for loop and we should continue from the next iteration of the inner for loop.
Hence in the above results, we can see that when the value of i == 3 or i == 4 the sysout statement of the inner for loop is not executed and the control goes to the next iteration of the inner for loop.
The label represents the name of the block. We can use the label in a continue statement to resume from a particular block.
In the above section, we have seen when we execute a continue statement the subsequent steps of the inner block are skipped and the control goes to the next iteration of the inner for loop. But what if we want to continue the block according to our requirements? Below are our requirements.
Let us understand it better with an example.
Write a program to continue the next iteration of the inner for loop with a label ( same as the above section but with a label )
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { B1: for (int i=1; i<=5; i++) { B2: for (int j=1; j<=5; j++) { if (i==3 || i==4) { System.out.println("Inside if condition, value of i : "+i+ " and value of j : "+j); continue B2; } System.out.println("Inner for loop outside if condition, value of i : "+i+ " and value of j : "+j); } } System.out.println("Control is outside of the outer for loop"); } } Output: Inner for loop outside if condition, value of i : 1 and value of j : 1 Inner for loop outside if condition, value of i : 1 and value of j : 2 Inner for loop outside if condition, value of i : 1 and value of j : 3 Inner for loop outside if condition, value of i : 1 and value of j : 4 Inner for loop outside if condition, value of i : 1 and value of j : 5 Inner for loop outside if condition, value of i : 2 and value of j : 1 Inner for loop outside if condition, value of i : 2 and value of j : 2 Inner for loop outside if condition, value of i : 2 and value of j : 3 Inner for loop outside if condition, value of i : 2 and value of j : 4 Inner for loop outside if condition, value of i : 2 and value of j : 5 Inside if condition, value of i : 3 and value of j : 1 Inside if condition, value of i : 3 and value of j : 2 Inside if condition, value of i : 3 and value of j : 3 Inside if condition, value of i : 3 and value of j : 4 Inside if condition, value of i : 3 and value of j : 5 Inside if condition, value of i : 4 and value of j : 1 Inside if condition, value of i : 4 and value of j : 2 Inside if condition, value of i : 4 and value of j : 3 Inside if condition, value of i : 4 and value of j : 4 Inside if condition, value of i : 4 and value of j : 5 Inner for loop outside if condition, value of i : 5 and value of j : 1 Inner for loop outside if condition, value of i : 5 and value of j : 2 Inner for loop outside if condition, value of i : 5 and value of j : 3 Inner for loop outside if condition, value of i : 5 and value of j : 4 Inner for loop outside if condition, value of i : 5 and value of j : 5 Control is outside of the outer for loop
Write a program to continue the next iteration of the outer for loop and skip the inner loop
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { B1: for (int i=1; i<=5; i++) { B2: for (int j=1; j<=5; j++) { if (i==3 || i==4) { System.out.println("Inside if condition, value of i : "+i+ " and value of j : "+j); continue B1; } System.out.println("Inner for loop outside if condition, value of i : "+i+ " and value of j : "+j); } } System.out.println("Control is outside of the outer for loop"); } } Output: Inner for loop outside if condition, value of i : 1 and value of j : 1 Inner for loop outside if condition, value of i : 1 and value of j : 2 Inner for loop outside if condition, value of i : 1 and value of j : 3 Inner for loop outside if condition, value of i : 1 and value of j : 4 Inner for loop outside if condition, value of i : 1 and value of j : 5 Inner for loop outside if condition, value of i : 2 and value of j : 1 Inner for loop outside if condition, value of i : 2 and value of j : 2 Inner for loop outside if condition, value of i : 2 and value of j : 3 Inner for loop outside if condition, value of i : 2 and value of j : 4 Inner for loop outside if condition, value of i : 2 and value of j : 5 Inside if condition, value of i : 3 and value of j : 1 Inside if condition, value of i : 4 and value of j : 1 Inner for loop outside if condition, value of i : 5 and value of j : 1 Inner for loop outside if condition, value of i : 5 and value of j : 2 Inner for loop outside if condition, value of i : 5 and value of j : 3 Inner for loop outside if condition, value of i : 5 and value of j : 4 Inner for loop outside if condition, value of i : 5 and value of j : 5 Control is outside of the outer for loop
In the above program, we have written an Outer for loop with label B1 where i should iterate from 1 to 5.
We have also written an Inner for loop with label B2 that will iterate 5 times for each iteration of i.
But we have also added an if condition which says if i == 3 or i == 4 then we should continue from the next iteration of the B1 loop and skip the B2 loop completely.
Hence in the above results, we can see that when the value of i == 3 or i == 4 the sysout statement of the inner for loop is not executed and the control goes to the next iteration of the outer for loop.
So this is all about continue statement in Java. I hope you like the article. If you have any questions on this topic please raise them in the comments section.
Please share this post and this website with your friends and colleagues so that they can learn Java and its frameworks in more depth.