{"id":1172,"date":"2022-02-24T19:31:22","date_gmt":"2022-02-24T19:31:22","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1172"},"modified":"2022-02-24T19:31:22","modified_gmt":"2022-02-24T19:31:22","slug":"java-continue-statement","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/java-continue-statement\/","title":{"rendered":"Java continue Statement"},"content":{"rendered":"\n<p>In this tutorial, you will learn about the continue statement and labeled continue statement in Java with the help of examples.<\/p>\n\n\n\n<p>While working with loops, sometimes you might want to skip some statements or terminate the loop. In such cases,&nbsp;<code>break<\/code>&nbsp;and&nbsp;<code>continue<\/code>&nbsp;statements are used.<\/p>\n\n\n\n<p>To learn about the\u00a0<code>break<\/code>\u00a0statement, visit\u00a0Java break. Here, we will learn about the\u00a0<code>continue<\/code>\u00a0statement.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"continue\">Java continue<\/h2>\n\n\n\n<p>The&nbsp;<code>continue<\/code>&nbsp;statement skips the current iteration of a loop (<code>for<\/code>,&nbsp;<code>while<\/code>,&nbsp;<code>do...while<\/code>, etc).<\/p>\n\n\n\n<p>After the&nbsp;<code>continue<\/code>&nbsp;statement, the program moves to the end of the loop. And, test expression is evaluated (update statement is evaluated in case of the for loop).<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the continue statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>continue;<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: The continue statement is almost always used in decision-making statements (if&#8230;else Statement).<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"working-of-continue\">Working of Java continue statement<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-continue.png\" alt=\"The working of continue statement with Java while, do...while, and for loop.\" title=\"Working of Java continue statement\"\/><figcaption>Working of Java continue Statement<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"continue-example\">Example 1: Java continue statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ for loop\n    for (int i = 1; i &lt;= 10; ++i) {\n\n      \/\/ if value of i is between 4 and 9\n      \/\/ continue is executed\n      if (i &gt; 4 &amp;&amp; i &lt; 9) {\n        continue;\n      }\n      System.out.println(i);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>1\n2\n3\n4\n9\n10<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we are using the\u00a0<code>for<\/code>\u00a0loop to print the value of\u00a0<var>i<\/var>\u00a0in each iteration. To know how\u00a0<code>for<\/code>\u00a0loop works, visit\u00a0Java for loop. Notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (i &gt; 4 &amp;&amp; i &lt; 9) {\n    continue;\n}<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>continue<\/code>&nbsp;statement is executed when the value of&nbsp;<var>i<\/var>&nbsp;becomes more than&nbsp;<strong>4<\/strong>&nbsp;and less than&nbsp;<strong>9<\/strong>.<\/p>\n\n\n\n<p>It then skips the print statement for those values. Hence, the output skips the values&nbsp;<strong>5<\/strong>,&nbsp;<strong>6<\/strong>,&nbsp;<strong>7<\/strong>, and&nbsp;<strong>8<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Compute the sum of 5 positive numbers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    Double number, sum = 0.0;\n    \/\/ create an object of Scanner\n    Scanner input = new Scanner(System.in);\n\n    for (int i = 1; i &lt; 6; ++i) {\n      System.out.print(\"Enter number \" + i + \" : \");\n      \/\/ takes input from the user\n      number = input.nextDouble();\n\n      \/\/ if number is negative\n      \/\/ continue statement is executed\n      if (number &lt;= 0.0) {\n        continue;\n      }\n\n      sum += number;\n    }\n    System.out.println(\"Sum = \" + sum);\n    input.close();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Enter number 1: 2.2\nEnter number 2: 5.6\nEnter number 3: 0\nEnter number 4: -2.4\nEnter number 5: -3\nSum = 7.8<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the for loop to print the sum of 5 positive numbers. Notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (number &lt; 0.0) {\n    continue;\n}<\/code><\/pre>\n\n\n\n<p>Here, when the user enters a negative number, the&nbsp;<code>continue<\/code>&nbsp;statement is executed. This skips the current iteration of the loop and takes the program control to the update expression of the loop.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"continue-nested-loops\">Java continue with Nested Loop<\/h2>\n\n\n\n<p>In the case of\u00a0nested loops in Java, the\u00a0<code>continue<\/code>\u00a0statement skips the current iteration of the innermost loop.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-continue-with-nested-loop.png\" alt=\"The continue statement skips the innermost loop while working with the nested loop in Java. \" title=\"Working of the continue Statement with Nested Loops\"\/><figcaption>Working of Java continue statement with Nested Loops<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-continue-nested\">Example 3: continue with Nested Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    int i = 1, j = 1;\n\n    \/\/ outer loop\n    while (i &lt;= 3) {\n\n      System.out.println(\"Outer Loop: \" + i);\n\n      \/\/ inner loop\n      while(j &lt;= 3) {\n\n        if(j == 2) {\n          j++;\n          continue;\n        }\n\n        System.out.println(\"Inner Loop: \" + j);\n        j++;\n      }\n      i++;\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Outer Loop: 1\nInner Loop: 1\nInner Loop: 3\nOuter Loop: 2\nOuter Loop: 3<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the nested\u00a0while loop. Note that we have used the\u00a0<code>continue<\/code>\u00a0statement inside the inner loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(j == 2) {\n  j++;\n  continue:\n}<\/code><\/pre>\n\n\n\n<p>Here, when the value of&nbsp;<var>j<\/var>&nbsp;is&nbsp;<strong>2<\/strong>, the value of&nbsp;<var>j<\/var>&nbsp;is increased and the&nbsp;<code>continue<\/code>&nbsp;statement is executed.<\/p>\n\n\n\n<p>This skips the iteration of the inner loop. Hence, the text&nbsp;<var>Inner Loop: 2<\/var>&nbsp;is skipped from the output.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"labeled-continue\">Labeled continue Statement<\/h2>\n\n\n\n<p>Till now, we have used the unlabeled&nbsp;<code>continue<\/code>&nbsp;statement. However, there is another form of&nbsp;<code>continue<\/code>&nbsp;statement in Java known as&nbsp;<strong>labeled continue<\/strong>.<\/p>\n\n\n\n<p>It includes the label of the loop along with the&nbsp;<code>continue<\/code>&nbsp;keyword. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>continue label;<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>continue<\/code>&nbsp;statement skips the current iteration of the loop specified by&nbsp;<var>label<\/var>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-labeled-continue.png\" alt=\"The labeled continue statement skips the current iteration of the loop specified by the label.\" title=\"Working of the labeled continue Statement in Java\"\/><figcaption>Working of the Java labeled continue Statement<\/figcaption><\/figure>\n\n\n\n<p>We can see that the label identifier specifies the outer loop. Notice the use of the continue inside the inner loop.<\/p>\n\n\n\n<p>Here, the&nbsp;<code>continue<\/code>&nbsp;statement is skipping the current iteration of the labeled statement (i.e. outer loop). Then, the program control goes to the next iteration of the labeled statement.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-labeled-continue\">Example 4: labeled continue Statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ outer loop is labeled as first\n    first:\n    for (int i = 1; i &lt; 6; ++i) {\n\n      \/\/ inner loop\n      for (int j = 1; j &lt; 5; ++j) {\n        if (i == 3 || j == 2)\n\n          \/\/ skips the current iteration of outer loop\n          continue first;\n        System.out.println(\"i = \" + i + \"; j = \" + j);\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>i = 1; j = 1\ni = 2; j = 1\ni = 4; j = 1\ni = 5; j = 1<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, the&nbsp;<code>labeled continue<\/code>&nbsp;statement is used to skip the current iteration of the loop labeled as&nbsp;<var>first<\/var>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (i==3 || j==2)\n    continue first;<\/code><\/pre>\n\n\n\n<p>Here, we can see the outermost&nbsp;<code>for<\/code>&nbsp;loop is labeled as&nbsp;<var>first<\/var>,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>first:\nfor (int i = 1; i &lt; 6; ++i) {..}<\/code><\/pre>\n\n\n\n<p>Hence, the iteration of the outer&nbsp;<code>for<\/code>&nbsp;loop is skipped if the value of&nbsp;<var>i<\/var>&nbsp;is 3 or the value of&nbsp;<var>j<\/var>&nbsp;is 2.<\/p>\n\n\n\n<p><strong>Note<\/strong>: The use of labeled\u00a0<code>continue<\/code>\u00a0is often discouraged as it makes your code hard to understand. If you are in a situation where you have to use labeled\u00a0<code>continue<\/code>, refactor your code and try to solve it in a different way to make it more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the continue statement and labeled continue statement in Java with the help of examples. While working with loops, sometimes you might want to skip some statements or terminate the loop. In such cases,&nbsp;break&nbsp;and&nbsp;continue&nbsp;statements are used. To learn about the\u00a0break\u00a0statement, visit\u00a0Java break. Here, we will learn about the\u00a0continue\u00a0statement. Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1172"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1172"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1172\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}