Site icon C1CTech

Kotlin break

<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>Kotlin 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&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>If <strong><span style&equals;"color&colon; &num;000080&semi;">test-expression<&sol;span> <&sol;strong>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;000080&semi;">break<&sol;span> <&sol;strong>is executed which terminates the for loop&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; for &lpar;n in 1&period;&period;10&rpar; &lbrace;&NewLine; if &lpar;n &equals;&equals; 5&rpar; &lbrace;&NewLine; break&NewLine; &rcub;&NewLine; println&lpar;"&dollar;n"&rpar;&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 n&equals; 5<&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;n &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>Kotlin 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>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; for &lpar;i in 1&period;&period;3&rpar; &lbrace;&NewLine; for &lpar;j in 1&period;&period;2&rpar; &lbrace;&NewLine; println&lpar;"i&equals;&dollar;i and j&equals;&dollar;j"&rpar;&NewLine; if &lpar;i &equals;&equals; 2&rpar;&NewLine; break&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><strong><span style&equals;"color&colon; &num;008000&semi;">i&equals;2 and j&equals;1<&sol;span><&sol;strong> gets printed&period;<&sol;li>&NewLine;<li>Then&comma; <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 &equals;&equals; 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; 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>Kotlin 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 <strong><span style&equals;"color&colon; &num;008000&semi;">labels<&sol;span><&sol;strong>&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>&commat;<&sol;strong>&period; e&period;g&period;- <span style&equals;"color&colon; &num;008000&semi;"><strong>inner&commat;&comma; outer&commat;<&sol;strong><&sol;span> etc&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax<&sol;span><&sol;strong><&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&commat;<&sol;span><&sol;strong> for&lpar;iteration through iterator&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&commat;<&sol;span><&sol;strong> for&lpar;iteration through iterator&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; code<&sol;strong><&sol;span>&NewLine; if&lpar;break condition&rpar; &lbrace;&NewLine; break<strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;outer<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine;&rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>Here&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">outer&commat;<&sol;span><&sol;strong> is a label marked at the outer for loop&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;<p>Now&comma; by using <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span> <&sol;strong>with a label &lpar;<strong><span style&equals;"color&colon; &num;0000ff&semi;">break&commat;outer<&sol;span><&sol;strong> 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><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">outer&commat;<&sol;span><&sol;strong> for &lpar;i in 1&period;&period;3&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>inner&commat;<&sol;strong><&sol;span> for &lpar;j in 1&period;&period;2&rpar; &lbrace;&NewLine; println&lpar;"i&equals;&dollar;i and j&equals;&dollar;j"&rpar;&NewLine; if &lpar;i &equals;&equals; 2&rpar;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">break&commat;outer<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine; println&lpar;"Outside inner loop"&rpar;&NewLine; &rcub;&NewLine; println&lpar;"Outside outer loop"&rpar;&NewLine;&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 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 <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period; The <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;">break&commat;outer<&sol;span><&sol;strong> is executed which terminates the desired loop marked with label <strong><span style&equals;"color&colon; &num;008000&semi;">outer&commat;<&sol;span><&sol;strong>&period;<&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 <strong><span style&equals;"color&colon; &num;008000&semi;">inner&commat;<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> outer&commat;<&sol;span><&sol;strong> for &lpar;i in 1&period;&period;3&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">inner&commat;<&sol;span><&sol;strong> for &lpar;j in 1&period;&period;2&rpar; &lbrace;&NewLine; println&lpar;"i&equals;&dollar;i and j&equals;&dollar;j"&rpar;&NewLine; if &lpar;i &equals;&equals; 2&rpar;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>break&commat;inner<&sol;strong><&sol;span>&NewLine; &rcub;&NewLine; println&lpar;"Outside inner loop"&rpar;&NewLine; &rcub;&NewLine; println&lpar;"Outside outer loop"&rpar;&NewLine;&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 <strong><span style&equals;"color&colon; &num;0000ff&semi;">&lpar;i &equals;&equals; 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; The <strong><span style&equals;"color&colon; &num;008000&semi;">break&commat;inner<&sol;span><&sol;strong> is executed which terminates the loop marked with label<strong><span style&equals;"color&colon; &num;008000&semi;"> inner&commat;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><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;0000ff&semi;">Note&colon;<&sol;span><&sol;strong> 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;<&sol;span><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version