Site icon C1CTech

Java continue Statement

<p>In this article&comma; you will learn how to use continue construct and continue labels in Java&period;<&sol;p>&NewLine;<p>Sometimes it is useful to force an early iteration of a loop&period; That is&comma; you might want to continue running the loop but stop processing the remaining code in its body for this particular iteration&period; In such case we can use <span style&equals;"color&colon; &num;008000&semi;"><strong>continue<&sol;strong> <&sol;span>statement&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java continue example<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The continue statement is used to skip the iteration of the nearest enclosing loop &lpar;for&comma; while&comma; and do…while loop&rpar;&period;<&sol;p>&NewLine;<p>Basically&comma; continue is used to repeat the loop for a specific condition&period; It skips the remaining statements and continues with the next iteration of the loop&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Syntax of <span style&equals;"color&colon; &num;008000&semi;"><strong>continue<&sol;strong> <&sol;span>in for loop&colon;<&sol;p>&NewLine;<pre>for &lpar;&period;&period;&period;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;codes<&sol;span><&sol;strong>&NewLine; if &lpar;test-expression&rpar; &lbrace;&NewLine; continue&semi;&NewLine; &rcub;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;codes<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p>If<span style&equals;"color&colon; &num;0000ff&semi;"><strong> test-expression <&sol;strong><&sol;span>is evaluated to <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">continue<&sol;span> <&sol;strong>statement is executed which skips the remaining statements inside the loop and continues with the next iteration of the loop&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>The below program will print even numbers between 1 to 10&period;<&sol;p>&NewLine;<pre>public class ContinueDemo &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; for &lpar;int n &equals; 1&semi; n &lt&semi;&equals; 10&semi; n&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; if &lpar;n &percnt; 2 &excl;&equals; 0&rpar;&NewLine; continue&semi;&NewLine;&NewLine; System&period;out&period;print&lpar;" " &plus; n &plus; " "&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre> 2  4  6  8  10<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>In the above example&comma; when n &equals; &lbrace; 1&comma; 3&comma; 5&comma; 7&comma; 9 &rcub; &colon;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>If<&sol;strong><&sol;span> condition<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &lpar; n &percnt; 2 &excl; &equals; 0 &rpar;<&sol;span><&sol;strong> is checked<strong>&comma; <&sol;strong>yields <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span>&period;<&sol;strong>Then<strong>&comma; <&sol;strong>continue statement is executed&comma; which skips the execution of <strong><span style&equals;"text-align&colon; left&semi; color&colon; &num;008000&semi; text-transform&colon; none&semi; line-height&colon; 1&period;2em&semi; text-indent&colon; 0px&semi; letter-spacing&colon; normal&semi; font-family&colon; 'Noto Serif'&comma;Georgia&comma;'Times New Roman'&comma;Times&comma;serif&semi; font-size&colon; 16px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; text-decoration&colon; none&semi; word-spacing&colon; 0px&semi; display&colon; inline &excl;important&semi; white-space&colon; normal&semi; cursor&colon; text&semi; orphans&colon; 2&semi; float&colon; none&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; &num;ffffff&semi;">System&period;out&period;print&lpar;&&num;8221&semi; &&num;8221&semi; &plus; n &plus; &&num;8221&semi; &&num;8220&semi;&rpar;<&sol;span><&sol;strong> statement&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java continue example in nested loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<div>When <strong><span style&equals;"color&colon; &num;008000&semi;">continue<&sol;span> <&sol;strong>is used in the nested loop &lpar;loop inside another loop&rpar;&comma; it skips the iteration of the nearest enclosing loop &lpar;inner loop&rpar; when it is encountered and continues with the next iteration of the loop&period;<&sol;div>&NewLine;<div>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class ContinueDemo &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; 3&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; for &lpar;int j &equals; 1&semi; j &lt&semi;&equals; 2&semi; j&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; System&period;out&period;println&lpar;"Before continue&comma;i &equals; " &plus; i &plus; "&semi; j &equals; " &plus;j&rpar;&semi;&NewLine; if &lpar;i &equals;&equals; 2&rpar; &lbrace;&NewLine; continue&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"i &equals; " &plus; i &plus; "&semi; j &equals; " &plus;j&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>Before continue&comma;i &equals; 1&semi; j &equals; 1&NewLine;i &equals; 1&semi; j &equals; 1&NewLine;Before continue&comma;i &equals; 1&semi; j &equals; 2&NewLine;i &equals; 1&semi; j &equals; 2&NewLine;Before continue&comma;i &equals; 2&semi; j &equals; 1&NewLine;Before continue&comma;i &equals; 2&semi; j &equals; 2&NewLine;Before continue&comma;i &equals; 3&semi; j &equals; 1&NewLine;i &equals; 3&semi; j &equals; 1&NewLine;Before continue&comma;i &equals; 3&semi; j &equals; 2&NewLine;i &equals; 3&semi; j &equals; 2<&sol;pre>&NewLine;<div>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<&sol;div>&NewLine;<p>In the above example&comma; when i &equals; 2 &colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>If<&sol;strong><&sol;span> condition<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &lpar; i &equals;&equals; 2 &rpar;<&sol;span><&sol;strong> is checked<strong>&comma; <&sol;strong>yields <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span>&period;<&sol;strong>Then<strong>&comma; <&sol;strong>continue statement is executed&comma; which skips the execution of <span style&equals;"display&colon; inline &excl;important&semi; float&colon; none&semi; background-color&colon; &num;ffffff&semi; color&colon; &num;2c3338&semi; cursor&colon; text&semi; font-family&colon; 'Noto Serif'&comma;Georgia&comma;'Times New Roman'&comma;Times&comma;serif&semi; font-size&colon; 16px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; 400&semi; letter-spacing&colon; normal&semi; line-height&colon; 1&period;2em&semi; orphans&colon; 2&semi; text-align&colon; left&semi; text-decoration&colon; none&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; -webkit-text-stroke-width&colon; 0px&semi; white-space&colon; normal&semi; word-spacing&colon; 0px&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">System&period;out&period;println&lpar;&&num;8220&semi;i &equals; &&num;8221&semi; &plus; i &plus; &&num;8220&semi;&semi; j &equals; &&num;8221&semi; &plus;j&rpar;<&sol;span><&sol;strong><&sol;span> statement and passes the control to inner for-loop for further iteration&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java labeled continue example<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>In Kotlin&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>labeled<&sol;strong> <&sol;span>continue is used to skip the iteration of the desired loop when certain condition is satisfied&period; It can be done with the help of <span style&equals;"color&colon; &num;008000&semi;"><strong>labels<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Label<&sol;span> <&sol;strong>starts with an identifier which is followed by <strong>&OpenCurlyQuote; &colon; &OpenCurlyQuote;<&sol;strong>&period; e&period;g&period;-<span style&equals;"color&colon; &num;008000&semi;"><strong> inner&colon; &comma; outer&colon;<&sol;strong><&sol;span> etc&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Syntax of labeled <span style&equals;"color&colon; &num;008000&semi;"><strong>continue<&sol;strong> <&sol;span>in for loop&colon;<&sol;p>&NewLine;<pre>outer&colon;&NewLine;for &lpar;&period;&period;&period;&rpar;&lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; code<&sol;strong><&sol;span>&NewLine; inner&colon;&NewLine; for &lpar;&period;&period;&period;&rpar;&lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; code<&sol;span><&sol;strong>&NewLine; if &lpar;condition for continue&rpar;&lbrace;&NewLine; continue outer&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>public class ContinueDemo &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; label&colon;&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; 5&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; for &lpar;int j &equals; 1&semi; j &lt&semi;&equals; 2&semi; j&plus;&plus;&rpar; &lbrace;&NewLine; if &lpar;i &gt&semi; 2&rpar; &lbrace;&NewLine; continue label&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"i &equals; " &plus; i &plus; "&semi; j &equals; " &plus;j&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>i &equals; 1&semi; j &equals; 1&NewLine;i &equals; 1&semi; j &equals; 2&NewLine;i &equals; 2&semi; j &equals; 1&NewLine;i &equals; 2&semi; j &equals; 2<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>In the above example&comma; when i &equals; &lbrace; 3&comma; 4&comma; 5 &rcub; &colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">If<&sol;span><&sol;strong> condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">&lpar; i &gt&semi; 2 &rpar;<&sol;span><&sol;strong> is checked&comma; yields <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period;Then&comma;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> continue label<&sol;span><&sol;strong> is executed&comma; which skips the execution of<strong><span style&equals;"color&colon; &num;008000&semi;"> System&period;out&period;println&lpar;&&num;8220&semi;i &equals; &&num;8221&semi; &plus; i &plus; &&num;8220&semi;&semi; j &equals; &&num;8221&semi; &plus;j&rpar;<&sol;span><&sol;strong> statement and passes the control to the desired loop marked with label <strong><span style&equals;"color&colon; &num;0000ff&semi;">label&colon;<&sol;span><&sol;strong> for further iteration&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version