Site icon C1CTech

Java for Loop

<p>In this article&comma; you will learn about how to create for loop in Java programming with the help of examples&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Loops<&sol;span> <&sol;strong>in Java 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>Java for Loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>A <strong><span style&equals;"color&colon; &num;0000ff&semi;">for<&sol;span><&sol;strong> loop is used to repeat a specific block of code a <span style&equals;"color&colon; &num;008000&semi;"><b>known<&sol;b><&sol;span> number of times&period;<&sol;p>&NewLine;<p class&equals;"p1">It provides a concise way of writing the loop structure&period; Unlike a while loop&comma; a for statement contains the initialization&comma; test-expression and increment&sol;decrement in a single line&period;<&sol;p>&NewLine;<p>The for loop is mainly used when you know exactly how many times the loop is about to be executed i&period;e&period; with what values will it start and with what values it will end and what will be its test condition&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>for &lpar;initialization&semi; test-expression&semi; updation&rpar;&NewLine;&lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; body of the loop<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; statements to be executed<&sol;strong><&sol;span>&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1">The various <span style&equals;"color&colon; &num;000000&semi;">parts of the for loop<&sol;span> are&colon;<&sol;p>&NewLine;<p><b>1&period; <span style&equals;"color&colon; &num;0000ff&semi;">initialization<&sol;span> &colon;<&sol;b>  This expression allows you to declare and initialize any loop control variable and is executed first&comma; and only once&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<p><b><span style&equals;"color&colon; &num;008000&semi;">Example<&sol;span>&colon;   <&sol;b>int i &equals; 1<&sol;p>&NewLine;<p><b>2&period; <span style&equals;"color&colon; &num;0000ff&semi;">test-expression&colon;<&sol;span><&sol;b> In this expression&comma; we have to define the condition i&period;e&period; the number of times the body of the loop will execute&period;<&sol;p>&NewLine;<p><b><span style&equals;"color&colon; &num;008000&semi;">Example<&sol;span>&colon;  <&sol;b>i &lt&semi;&equals; 10<&sol;p>&NewLine;<p><b>3&period; <span style&equals;"color&colon; &num;0000ff&semi;">updation<&sol;span><&sol;b>&colon; This expression allows you to update &lpar;increment&sol;decrement&rpar; the loop control variable by some value and is executed &lpar;every time&rpar; after executing the loop body&period;<&sol;p>&NewLine;<p><b><span style&equals;"color&colon; &num;008000&semi;">Example<&sol;span>&colon;  <&sol;b>i&plus;&plus;<&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>How for loop works&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">When the <strong><span style&equals;"color&colon; &num;008000&semi;">Control<&sol;span> <&sol;strong>falls into the for loop&comma; it first executes the initialization expression only once&period; Then&comma; checks the <span style&equals;"color&colon; &num;008000&semi;"><strong>test-expression<&sol;strong><&sol;span>&colon;<&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;<ol class&equals;"ul1">&NewLine;<li>The statements inside the body of <strong><span style&equals;"color&colon; &num;008000&semi;">for<&sol;span><&sol;strong> loop get executed&period;<&sol;li>&NewLine;<li class&equals;"li1">Then&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">Updation<&sol;span><&sol;strong> takes place&period;<&sol;li>&NewLine;<li class&equals;"li1">Again&comma; the <strong><span style&equals;"color&colon; &num;008000&semi;">test-expression<&sol;span><&sol;strong> is evaluated&comma; if it is true&comma; step 1 and step 2 get executed&period;<&sol;li>&NewLine;<li class&equals;"li1">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;ol>&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">for loop gets terminated and control goes out of the <strong><span style&equals;"color&colon; &num;008000&semi;">for<&sol;span><&sol;strong> loop&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Flowchart of for loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><img class&equals;" wp-image-1690 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-19-15&period;16&period;52-1&period;png" alt&equals;"Screenshot 2020-03-19 15&period;16&period;52" width&equals;"579" height&equals;"587" &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 ForLoop &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; 4&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"value of i&colon; " &plus; i&rpar;&semi;&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>value of i&colon; 1&NewLine;value of i&colon; 2&NewLine;value of i&colon; 3<&sol;pre>&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;"><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 <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;008000&semi;"><b>&OpenCurlyDoubleQuote;value of i&colon; 1”<&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 <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><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;"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 <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><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;"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 <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&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>The below program will print the sum of natural numbers from 1 to 10&period;<&sol;p>&NewLine;<pre>public class ForLoop &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; &NewLine; int 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; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; 10&semi; i&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; sum &equals; sum &plus; i&semi;&NewLine; &NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Sum of numbers from 1 to 10&colon; " &plus; sum&rpar;&semi;&NewLine;&NewLine; &rcub;&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>Sum of numbers from 1 to 10&colon; 55<&sol;pre>&NewLine;&NewLine;

Exit mobile version