Site icon C1CTech

Kotlin do-while Loop

<p>In this article&comma; you will learn about the do-while loop and how to create do-while loop in Kotlin programming&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Loops<&sol;strong><&sol;span> in Kotlin are used when we want to execute a block of statements repeatedly until a specific condition is met&lpar;condition is <span style&equals;"color&colon; &num;008000&semi;"><strong>false<&sol;strong><&sol;span>&rpar;&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Kotlin do-while Loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1"><span class&equals;"s1">Kotlin&&num;8217&semi;s<&sol;span> <span style&equals;"color&colon; &num;008000&semi;"><b>do-while loop<&sol;b><&sol;span> is an <span style&equals;"color&colon; &num;008000&semi;"><strong>e<&sol;strong><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;">xit-control loop<&sol;span>&comma; <&sol;b>which means unlike while loop&comma; the do-while loop check for the condition after executing the statements or the loop body&period;<&sol;p>&NewLine;<p class&equals;"p1">The do-while loop is similar to while loop with one main difference&period; The body of the do-while loop is executed at least once before the test-expression is checked&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong><&sol;span> the do-while loop will execute the statements at least once before any condition is checked&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>do &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;body of the loop<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;statements to be executed<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;test expression is a boolean expression<&sol;span><&sol;strong>&NewLine; while &lpar;test&lowbar;expression&rpar;&semi;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Flowchart of do-while loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><img class&equals;" wp-image-1672 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-18-14&period;19&period;30-3022851422-1584522057550&period;png" alt&equals;"Screenshot 2020-03-18 14&period;19&period;30" width&equals;"590" height&equals;"489" &sol;><&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>How <&sol;b><strong>do-while <&sol;strong><b>loop works&quest;<&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">When the Control falls into the do-while loop&comma; it first executes the body of the loop inside the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>do<&sol;strong><&sol;span> block and then it checks the <span style&equals;"color&colon; &num;008000&semi;"><strong>test&lowbar;expression<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><strong>If the test expression is evaluated to true&comma;<&sol;strong><&sol;span><&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li>The statements inside the do block&lpar;body of the loop&rpar; get executed&period;<&sol;li>&NewLine;<li class&equals;"li2">Then&comma; the test expression is evaluated again and this process goes on until the test expression is evaluated to <span style&equals;"color&colon; &num;008000&semi;"><strong>false<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><strong>If the test expression is evaluated to false&comma;<&sol;strong><&sol;span><&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 class&equals;"ul1">&NewLine;<li class&equals;"li2">do-while loop gets terminated and control goes out of the do-while loop&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example 1<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; var i &equals; 1&NewLine;&NewLine; do &lbrace;&NewLine; System&period;out&period;println&lpar;"value of i&colon; &dollar;i"&rpar;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;increment value of i by 1<&sol;strong><&sol;span>&NewLine; i&plus;&plus;&NewLine; &rcub;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;exit when i becomes greater than 3<&sol;span><&sol;strong>&NewLine; while &lpar;i &lt&semi; 4&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>value of i&colon; 1&NewLine;value of i&colon; 2&NewLine;value of i&colon; 3<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma;<&sol;p>&NewLine;<p class&equals;"p1"><strong>1<&sol;strong>&period;<span style&equals;"color&colon; &num;008000&semi;"><strong> i <&sol;strong><&sol;span>is initialized with value 1&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>2&period; <&sol;strong>Execute the loop body inside <span style&equals;"color&colon; &num;0000ff&semi;"><strong>do<&sol;strong><&sol;span> block&period;<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1"><span style&equals;"color&colon; &num;008000&semi;"><strong>&OpenCurlyDoubleQuote;value of i&colon; 1”<&sol;strong><&sol;span> gets printed&period;<&sol;li>&NewLine;<li class&equals;"p1">Updation is done&period; Now <span style&equals;"color&colon; &num;008000&semi;"><b>i &equals; 2<&sol;b><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1"><strong>3<&sol;strong>&period; Condition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>2 &lt&semi; 4<&sol;strong><&sol;span> is checked&comma; yields true&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>4<&sol;strong>&period; Execute the loop body inside <span style&equals;"color&colon; &num;0000ff&semi;"><strong>do<&sol;strong><&sol;span> block&period;<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1"><span style&equals;"color&colon; &num;008000&semi;"><b>&OpenCurlyDoubleQuote;value of i&colon; 2”<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<li class&equals;"p1">Updation is done&period; Now <span style&equals;"color&colon; &num;008000&semi;"><b>i &equals; 3<&sol;b><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong>5<&sol;strong>&period; Condition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>3&lt&semi; 4<&sol;strong><&sol;span> is checked&comma; yields true&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>6<&sol;strong>&period; Execute the loop body inside <span style&equals;"color&colon; &num;0000ff&semi;"><strong>do<&sol;strong><&sol;span> block&period;<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1"><span style&equals;"color&colon; &num;008000&semi;"><b>&OpenCurlyDoubleQuote;value of i&colon; 3”<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<li class&equals;"p1">Updation is done&period; Now <span style&equals;"color&colon; &num;008000&semi;"><b>i &equals; 4<&sol;b><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong>7<&sol;strong>&period; Condition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>4&lt&semi; 4<&sol;strong><&sol;span> is checked&comma; yields false&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>8<&sol;strong>&period; Exit from the do-while loop&period;<&sol;p>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example 2<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>The below program will print the sum of natural numbers from 1 to 10&period;<&sol;p>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; var i &equals; 1&NewLine; var sum &equals; 0&NewLine;&NewLine; do &lbrace;&NewLine; sum &equals; sum &plus; i&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Increment the value of i for<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;next iteration<&sol;span><&sol;strong>&NewLine; i&plus;&plus;&NewLine; &rcub; while &lpar;i &lt&semi;&equals; 10&rpar;&NewLine;&NewLine; println&lpar;"Sum of numbers from 1 to 10&colon; &dollar;sum"&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>Sum of numbers from 1 to 10&colon; 55<&sol;pre>&NewLine;&NewLine;

Exit mobile version