

{"id":123428,"date":"2024-10-23T18:00:40","date_gmt":"2024-10-23T12:30:40","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123428"},"modified":"2024-10-23T19:26:02","modified_gmt":"2024-10-23T13:56:02","slug":"java-continue-statement","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/","title":{"rendered":"Java Continue Statement with Examples"},"content":{"rendered":"<p>The continue statement in Java jumps immediately to the next iteration of a loop, skipping the remaining code in the current iteration. This allows you to selectively execute code within loop structures like for, while, and do-while loops.<\/p>\n<p>The Java continue statement is a vital control statement that allows you to alter the program execution flow in loops. It works somewhat like a break statement, but instead of terminating the loop entirely, it simply skips the remaining code in the current iteration and then continues with the next one.<\/p>\n<p><strong>Some common use cases of continue are:<\/strong><\/p>\n<ul>\n<li>Skipping an iteration when a specific condition is met<\/li>\n<li>Jumping to the next loop cycle when some exception occurs<\/li>\n<li>Avoiding execution of code blocks within loops<\/li>\n<\/ul>\n<p>When executed, the continue statement immediately jumps to the next iteration, skipping any remaining statements in the body of the loop in the current iteration. This, in effect, skips one cycle of the enclosing loop structure.<\/p>\n<p>The continue statement can be used with all types of loops in Java &#8211; for loops, while loops and do-while loops.<\/p>\n<h2>Syntax of Java Continue Statement<\/h2>\n<p><strong>The syntax of the continue statement is straightforward:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Continue;<\/pre>\n<p>The continue keyword is followed by a semicolon. It does not need any condition or expression.<\/p>\n<h3>Example: Continue in For Loop<\/h3>\n<p><strong>Here is a simple example demonstrating the use of continue in a for loop:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ContinueExample {\r\n    public static void main(String[] args) {\r\n        for (int i = 1; i &lt;= 10; i++) {\r\n            if (i == 5) {\r\n                continue;\r\n            }\r\n            System.out.print(i + \" \");\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n1 2 3 4 6 7 8 9 10<\/p>\n<p>In this loop, the continue statement is executed when the value of i becomes 5. The loop then jumps back to the beginning, skipping the print statement for this iteration.<\/p>\n<h3>Continue in Nested Loops<\/h3>\n<p>The continue statement can also be used in nested loops with an inner loop and an outer loop.<\/p>\n<p><strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ContinueExample2 {\r\n    public static void main(String[] args) {\r\n        for (int i = 1; i &lt;= 3; i++) {\r\n            for (int j = 1; j &lt;= 3; j++) {\r\n                if (i == 2 &amp;&amp; j == 2) {\r\n                    continue;\r\n                }\r\n                System.out.println(i + \" \" + j);\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>Here, when i = 2 and j = 2, the continue statement skips the current iteration of the inner loop and continues with the next value of j.<\/p>\n<p><strong>Output:<\/strong><br \/>\n1 1<br \/>\n1 2<br \/>\n1 3<br \/>\n2 1<br \/>\n2 3<br \/>\n3 1<br \/>\n3 2<br \/>\n3 3<\/p>\n<p>So, the continue statement skipped the iteration for i = 2 and j = 2 in the inner loop.<\/p>\n<h3>Continue with Labeled Loops<\/h3>\n<p>From JDK 1.5 onwards, Java also supports labeled continue statements. This allows you to continue an outer loop instead of the innermost one selectively.<\/p>\n<p><strong>The syntax is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">label: \r\n\r\nfor () {\r\n\r\n  \/\/inner loop\r\n\r\n  continue label;\r\n\r\n}<\/pre>\n<p><strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ContinueExample3 {\r\n    public static void main(String[] args) {\r\n        outer: for (int i = 1; i &lt;= 3; i++) {\r\n            for (int j = 1; j &lt;= 3; j++) {\r\n                if (i == 2 &amp;&amp; j == 2) {\r\n                    continue outer;\r\n                }\r\n                System.out.println(i + \" \" + j);\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>Now, the continue statement with the label outer will continue the outer loop instead of the inner loop.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>1 1<br \/>\n1 2<br \/>\n1 3<br \/>\n3 1<br \/>\n3 2<br \/>\n3 3<\/p>\n<p>Here, the entire iteration for i = 2 was skipped.<\/p>\n<h3>Continue in While Loop<\/h3>\n<p><strong>The continue statement can also be used in while loops:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ContinueWhileExample {\r\n    public static void main(String[] args) {\r\n        int i = 0;\r\n        while (i &lt; 10) {\r\n            i++;\r\n            if (i == 5) {\r\n                continue;\r\n            }\r\n            System.out.print(i + \" \");\r\n        }\r\n    }\r\n}<\/pre>\n<p>This will print from 1 to 10, except 5.<\/p>\n<p>When i becomes 5, the continue statement executes, skipping the remaining statements in that iteration and jumping back to the start of the loop for the next iteration.<\/p>\n<p><strong>Output:<\/strong><br \/>\n1 2 3 4 6 7 8 9 10<\/p>\n<h3>Continue in Do-While Loop<\/h3>\n<p><strong>Similarly, you can use continue in do-while loops as well:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ContinueDoWhileExample {\r\n    public static void main(String[] args) {\r\n        int i = 0;\r\n        do {\r\n            i++;\r\n            if (i == 5) {\r\n                continue;\r\n            }\r\n            System.out.print(i + \" \");\r\n        } while (i &lt; 10);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n1 2 3 4 6 7 8 9 10<\/p>\n<p>Here, the continue statement works identically as in the while loop, skipping over the print statement for just the iteration when i = 5.<\/p>\n<h3>Conclusion<\/h3>\n<p>The continue statement is a helpful tool for loop control in Java. It immediately jumps to the next iteration of a loop based on a condition, allowing selective execution of code statements within loop structures.<\/p>\n<p>The Java continue statement works with for, while, and do-while loops. Labeled continues can also be used selectively to jump outer loops. This provides fine-grained control over complex nested loop structures.<\/p>\n<p>Understanding the continue statement is essential for efficient Java programming and control flow management in iterative logic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The continue statement in Java jumps immediately to the next iteration of a loop, skipping the remaining code in the current iteration. This allows you to selectively execute code within loop structures like for,&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":124273,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[19212,31086,7345,7446,31087,31078,8152,33189],"class_list":["post-123428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-continue-statement","tag-continue-statement-in-java","tag-java","tag-java-continue-statement","tag-java-continue-statement-with-examples","tag-java-tutorials","tag-learn-java","tag-syntax-of-continue-statement-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Continue Statement with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"The continue statement is an important control statement that allows you to alter the regular flow of program execution in loops.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-continue-statement\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Continue Statement with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The continue statement is an important control statement that allows you to alter the regular flow of program execution in loops.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-continue-statement\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-23T12:30:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-23T13:56:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-continue-statement.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Continue Statement with Examples - DataFlair","description":"The continue statement is an important control statement that allows you to alter the regular flow of program execution in loops.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/","og_locale":"en_US","og_type":"article","og_title":"Java Continue Statement with Examples - DataFlair","og_description":"The continue statement is an important control statement that allows you to alter the regular flow of program execution in loops.","og_url":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-10-23T12:30:40+00:00","article_modified_time":"2024-10-23T13:56:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-continue-statement.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Java Continue Statement with Examples","datePublished":"2024-10-23T12:30:40+00:00","dateModified":"2024-10-23T13:56:02+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/"},"wordCount":554,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-continue-statement.webp","keywords":["Continue statement","continue statement in java","Java","Java continue Statement","java continue statement with examples","java tutorials","Learn Java","syntax of continue statement in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-continue-statement\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/","url":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/","name":"Java Continue Statement with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-continue-statement.webp","datePublished":"2024-10-23T12:30:40+00:00","dateModified":"2024-10-23T13:56:02+00:00","description":"The continue statement is an important control statement that allows you to alter the regular flow of program execution in loops.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-continue-statement\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-continue-statement.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-continue-statement.webp","width":1200,"height":628,"caption":"java continue statement"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-continue-statement\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java Continue Statement with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123428"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123428\/revisions"}],"predecessor-version":[{"id":143536,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123428\/revisions\/143536"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/124273"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}