Site icon C1CTech

Java break Statement

<p>In this tutorial&comma; you will learn to use break to terminate a loop&period; Also&comma; you will learn about break labels&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java break example<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><strong>break <&sol;strong><&sol;span>is a loop control statement which is used to terminate the loop&period;<&sol;p>&NewLine;<div>Basically break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition&period;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>As soon as the break statement is encountered&comma; it terminates the nearest enclosing loop and control returns from the loop immediately to the first statement after the loop&period;<&sol;div>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Syntax of break in for loop&colon;<&sol;p>&NewLine;<pre>for &lpar;&period;&period;&period;&rpar; &lbrace;&NewLine; if &lpar;test-expression&rpar; &lbrace;&NewLine; break&semi;&NewLine; &rcub;&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;0000ff&semi;">break<&sol;span> <&sol;strong>is executed which terminates the <strong><span style&equals;"color&colon; &num;008000&semi;">for<&sol;span> <&sol;strong>loop&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class BreakDemo &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; 10&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; if &lpar;i &equals;&equals; 5&rpar; &lbrace;&NewLine; break&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;i&rpar;&semi;&NewLine; &rcub;&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>1&NewLine;2&NewLine;3&NewLine;4<&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; 5<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>if <&sol;strong><&sol;span>condition<span style&equals;"color&colon; &num;0000ff&semi;"><strong> &lpar;i &equals;&equals; 5&rpar;<&sol;strong><&sol;span> is checked&comma; yields <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&period; The break statement is executed which terminates the for loop&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java break example in nested loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<div><&sol;div>&NewLine;<div>When <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span> <&sol;strong>is used in the nested loop &lpar;loop inside another loop&rpar;&comma; it terminates the inner loop when it is encountered&period;<&sol;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 BreakDemo &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; System&period;out&period;println&lpar;"i &equals; " &plus; i &plus; " and j &equals; " &plus;j&rpar;&semi;&NewLine; if &lpar;i &equals;&equals; 2&rpar;&NewLine; break&semi;&NewLine; &rcub;&NewLine; &rcub;&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>i &equals; 1 and j &equals; 1&NewLine;i &equals; 1 and j &equals; 2&NewLine;i &equals; 2 and j &equals; 1&NewLine;i &equals; 3 and j &equals; 1&NewLine;i &equals; 3 and 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; 2&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>i&equals;2 and j&equals;1<&sol;strong><&sol;span> gets printed&period;<&sol;li>&NewLine;<li>Then&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">if<&sol;span> <&sol;strong>condition<span style&equals;"color&colon; &num;0000ff&semi;"><strong> &lpar;i &equals;&equals; 2&rpar;<&sol;strong><&sol;span> is checked&comma; yields <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&period; The break statement gets executed which terminates the inner for loop&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java Labeled break<&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>break is used to terminate 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><span style&equals;"color&colon; &num;0000ff&semi;">&&num;8216&semi; &colon; &&num;8216&semi;<&sol;span><&sol;strong>&period; e&period;g&period;-<strong><span style&equals;"color&colon; &num;008000&semi;"> inner&colon; &comma; outer&colon;<&sol;span><&sol;strong> etc&period;<&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;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Syntax of labeled break in for loop&colon;<&sol;p>&NewLine;<pre><strong><span style&equals;"color&colon; &num;0000ff&semi;">outer&colon;<&sol;span><&sol;strong>&NewLine;for &lpar;&period;&period;&period;&rpar;&lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; code<&sol;span>&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;">inner&colon;<&sol;span><&sol;strong>&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;break condition&rpar;&lbrace;&NewLine; break outer&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>Here&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>outer&colon;<&sol;strong><&sol;span> is a label marked at the outer for loop&period;<&sol;p>&NewLine;<p>Now&comma; by using <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span> <&sol;strong>with a label &lpar;<span style&equals;"color&colon; &num;008000&semi;"><strong>break outer<&sol;strong><&sol;span> in this case&rpar;&comma; breaks the specific loop&lpar;outer loop&rpar;&comma; when the break condition is evaluated to <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class BreakDemo &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>outer&colon;<&sol;strong><&sol;span>&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; 3&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">inner&colon;<&sol;span><&sol;strong>&NewLine; for &lpar;int j &equals; 1&semi; j &lt&semi;&equals; 2&semi; j&plus;&plus;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"i &equals; " &plus; i &plus; " and j &equals; " &plus; j&rpar;&semi;&NewLine; if &lpar;i &equals;&equals; 2&rpar;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">break outer&semi;<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Outside inner loop"&rpar;&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Outside outer loop"&rpar;&semi;&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>i &equals; 1 and j &equals; 1&NewLine;i &equals; 1 and j &equals; 2&NewLine;Outside inner loop&NewLine;i &equals; 2 and j &equals; 1&NewLine;Outside outer loop<&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; 2&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">if<&sol;span> <&sol;strong>condition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&lpar;i &equals;&equals; 2&rpar;<&sol;strong><&sol;span> is checked&comma; yields <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&period; The <span style&equals;"color&colon; &num;008000&semi;"><strong>break outer<&sol;strong><&sol;span> is executed which terminates the desired loop marked with label <span style&equals;"color&colon; &num;0000ff&semi;"><strong>outer&colon; <&sol;strong><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>In the below program&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span> <&sol;strong>terminates the loop marked with label <span style&equals;"color&colon; &num;0000ff&semi;"><strong>inner&colon; <&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class BreakDemo &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">outer&colon;<&sol;span><&sol;strong>&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; 3&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">inner&colon;<&sol;span><&sol;strong>&NewLine; for &lpar;int j &equals; 1&semi; j &lt&semi;&equals; 2&semi; j&plus;&plus;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"i &equals; " &plus; i &plus; " and j &equals; " &plus; j&rpar;&semi;&NewLine; if &lpar;i &equals;&equals; 2&rpar;&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>break inner&semi;<&sol;strong><&sol;span>&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Outside inner loop"&rpar;&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Outside outer loop"&rpar;&semi; &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>i &equals; 1 and j &equals; 1&NewLine;i &equals; 1 and j &equals; 2&NewLine;Outside inner loop&NewLine;i &equals; 2 and j &equals; 1&NewLine;Outside inner loop&NewLine;i &equals; 3 and j &equals; 1&NewLine;i &equals; 3 and j &equals; 2&NewLine;Outside inner loop&NewLine;Outside outer loop<&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; 2&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>if condition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&lpar;i &equals;&equals; 2&rpar;<&sol;strong><&sol;span> is checked&comma; yields <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&period; The <span style&equals;"color&colon; &num;008000&semi;"><strong>break inner<&sol;strong><&sol;span> is executed which terminates the loop marked with label<span style&equals;"color&colon; &num;0000ff&semi;"><strong> inner&colon;<&sol;strong><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong><&sol;span> Since&comma; break is used to terminate the innermost loop&comma; in the above program&comma; it is not necessary to use labeled break in this case&period;&NewLine;

Exit mobile version