Site icon C1CTech

Java while Loop

<p class&equals;"p1">In this article&comma; you will learn about while loop and how to create while loop in Java programming&period;<&sol;p>&NewLine;<p>Loops in Java are used when we want to execute a block of statements repeatedly until a specific condition is met&lpar;condition is <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&rpar;&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java While Loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>while<&sol;strong><&sol;span> loop executes the block of code as long as a specified condition is <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>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&sol;&sol;while loop&NewLine;<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;test&lowbar;expression is a boolean expression<&sol;strong><&sol;span>&NewLine;while &lpar;test&lowbar;expression&rpar; &lbrace;&NewLine;&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;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>How while loop works&quest;<&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">When the Control falls into the while loop&comma; it first checks the <strong><span style&equals;"color&colon; &num;008000&semi;">test&lowbar;expression <&sol;span><&sol;strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">&lpar;boolean expression&rpar;<&sol;span><&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 body of the loop 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 <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&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;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2">while loop gets terminated and control goes out of the while loop&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Flowchart<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;" wp-image-1670 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;21&period;18&period;png" alt&equals;"Screenshot 2020-03-18 14&period;21&period;18" width&equals;"562" height&equals;"509" &sol;><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example 1<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class WhileLoop &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &NewLine; &sol;&sol;initialization<&sol;span><&sol;strong>&NewLine; int i &equals; 1&semi;&NewLine;&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; &lbrace;&NewLine; System&period;out&period;println&lpar;"value of i&colon; " &plus;i&rpar;&semi;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;updation<&sol;span><&sol;strong>&NewLine; i&plus;&plus;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&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><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma;<&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 class&equals;"p1"><strong>1<&sol;strong>&period; <span style&equals;"color&colon; &num;008000&semi;"><b>i<&sol;b><&sol;span> is initialized with value 1&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>2<&sol;strong>&period; condition <span style&equals;"color&colon; &num;0000ff&semi;"><b>1 &lt&semi; 4<&sol;b><&sol;span> is checked&comma; yields true&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;008000&semi;"><b> &&num;8220&semi;value of i&colon; 1&&num;8221&semi;<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"Apple-converted-space">  <&sol;span>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;"><b>2 &lt&semi; 4<&sol;b><&sol;span> is checked&comma; yields true&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;008000&semi;"><b>&&num;8220&semi;value of i&colon; 2&&num;8221&semi;<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"Apple-converted-space">  <&sol;span>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 class&equals;"p1"><strong>4<&sol;strong>&period; condition <span style&equals;"color&colon; &num;0000ff&semi;"><b>3 &lt&semi; 4<&sol;b><&sol;span> is checked&comma; yields true&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;008000&semi;"><b>&&num;8220&semi;value of i&colon; 3&&num;8221&semi;<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"Apple-converted-space">  <&sol;span>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 class&equals;"p1"><strong>5<&sol;strong>&period; condition <span style&equals;"color&colon; &num;0000ff&semi;"><b>4 &lt&semi; 4<&sol;b><&sol;span> is checked&comma; yields false&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>6<&sol;strong>&period; exit from the 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 class&equals;"p1">The below program will print the sum of natural numbers from 1 to 10&period;<&sol;p>&NewLine;<pre>public class WhileLoop &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; int i &equals; 1&comma; sum &equals; 0&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; exit when i becomes greater than 10<&sol;span><&sol;strong>&NewLine; while &lpar;i &lt&semi;&equals; 10&rpar; &lbrace;&NewLine;&NewLine; sum &equals; sum &plus; i&semi; &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;&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Sum of numbers from 1 to 10&colon; " &plus; sum&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre class&equals;"p1">Sum of numbers from 1 to 10&colon; 55<&sol;pre>&NewLine;&NewLine;

Exit mobile version