{"id":1164,"date":"2022-02-24T19:21:21","date_gmt":"2022-02-24T19:21:21","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1164"},"modified":"2022-02-24T19:21:21","modified_gmt":"2022-02-24T19:21:21","slug":"java-for-loop","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/java-for-loop\/","title":{"rendered":"Java for Loop"},"content":{"rendered":"\n<p>In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming.<\/p>\n\n\n\n<p id=\"introduction\">In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then rather than typing the same code 100 times, you can use a loop.<\/p>\n\n\n\n<p id=\"introduction\">In Java, there are three types of loops.<\/p>\n\n\n\n<ul><li>for loop<\/li><li>while loop<\/li><li>do&#8230;while loop<\/li><\/ul>\n\n\n\n<p id=\"introduction\">This tutorial focuses on the for loop. You will learn about the other type of loops in the upcoming tutorials.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"for-loop\">Java for Loop<\/h2>\n\n\n\n<p>Java&nbsp;<code>for<\/code>&nbsp;loop is used to run a block of code for a certain number of times. The syntax of&nbsp;<code>for<\/code>&nbsp;loop is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (initialExpression;\n testExpression; \nupdateExpression) {\n    \/\/ body of the loop\n}<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ol><li>The&nbsp;<strong>initialExpression<\/strong>&nbsp;initializes and\/or declares variables and executes only once.<\/li><li>The&nbsp;<strong>condition<\/strong>&nbsp;is evaluated. If the&nbsp;<strong>condition<\/strong>&nbsp;is&nbsp;<code>true<\/code>, the body of the&nbsp;<code>for<\/code>&nbsp;loop is executed.<\/li><li>The&nbsp;<strong>updateExpression<\/strong>&nbsp;updates the value of&nbsp;<strong>initialExpression<\/strong>.<\/li><li>The&nbsp;<strong>condition<\/strong>&nbsp;is evaluated again. The process continues until the&nbsp;<strong>condition<\/strong>&nbsp;is&nbsp;<code>false<\/code>.<\/li><\/ol>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<figure class=\"wp-block-image\" id=\"flowchart\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-for-loop.png\" alt=\"Working of for loop in Java with flowchart\" title=\"Working of for loop in Java\"\/><figcaption>Flowchart of Java for loop<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\">Example 1: Display a Text Five Times<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Program to print a text 5 times\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    int n = 5;\n    \/\/ for loop  \n    for (int i = 1; i &lt;= n; ++i) {\n      System.out.println(\"Java is fun\");\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Java is fun\nJava is fun\nJava is fun\nJava is fun\nJava is fun<\/samp><\/code><\/pre>\n\n\n\n<p>Here is how this program works.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Iteration<\/th><th>Variable<\/th><th>Condition: i &lt;= n<\/th><th>Action<\/th><\/tr><tr><td>1st<\/td><td><code>i = 1<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>Java is fun<\/samp> is printed.<br><var>i<\/var> is increased to <strong>2<\/strong>.<\/td><\/tr><tr><td>2nd<\/td><td><code>i = 2<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>Java is fun<\/samp> is printed.<br><var>i<\/var> is increased to <strong>3<\/strong>.<\/td><\/tr><tr><td>3rd<\/td><td><code>i = 3<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>Java is fun<\/samp> is printed.<br><var>i<\/var> is increased to <strong>4<\/strong>.<\/td><\/tr><tr><td>4th<\/td><td><code>i = 4<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>Java is fun<\/samp> is printed.<br><var>i<\/var> is increased to <strong>5<\/strong>.<\/td><\/tr><tr><td>5th<\/td><td><code>i = 5<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>Java is fun<\/samp> is printed.<br><var>i<\/var> is increased to <strong>6<\/strong>.<\/td><\/tr><tr><td>6th<\/td><td><code>i = 6<\/code><br><code>n = 5<\/code><\/td><td><code>false<\/code><\/td><td>The loop is terminated.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Display numbers from 1 to 5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Program to print numbers from 1 to 5\n\nclass Main {\n  public static void main(String&#91;] args) {\n  \n    int n = 5;\n    \/\/ for loop  \n    for (int i = 1; i &lt;= n; ++i) {\n      System.out.println(i);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>1\n2\n3\n4\n5<\/samp><\/code><\/pre>\n\n\n\n<p>Here is how the program works.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Iteration<\/th><th>Variable<\/th><th>Condition: i &lt;= n<\/th><th>Action<\/th><\/tr><tr><td>1st<\/td><td><code>i = 1<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>1<\/samp> is printed.<br><var>i<\/var> is increased to <strong>2<\/strong>.<\/td><\/tr><tr><td>2nd<\/td><td><code>i = 2<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>2<\/samp> is printed.<br><var>i<\/var> is increased to <strong>3<\/strong>.<\/td><\/tr><tr><td>3rd<\/td><td><code>i = 3<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>3<\/samp> is printed.<br><var>i<\/var> is increased to <strong>4<\/strong>.<\/td><\/tr><tr><td>4th<\/td><td><code>i = 4<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>4<\/samp> is printed.<br><var>i<\/var> is increased to <strong>5<\/strong>.<\/td><\/tr><tr><td>5th<\/td><td><code>i = 5<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>5<\/samp> is printed.<br><var>i<\/var> is increased to <strong>6<\/strong>.<\/td><\/tr><tr><td>6th<\/td><td><code>i = 6<\/code><br><code>n = 5<\/code><\/td><td><code>false<\/code><\/td><td>The loop is terminated.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\">Example 3: Display Sum of n Natural Numbers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Program to find the sum of natural numbers from 1 to 1000.\n\nclass Main {\n  public static void main(String&#91;] args) {\n      \n    int sum = 0;\n    int n = 1000;\n\n    \/\/ for loop\n    for (int i = 1; i &lt;= n; ++i) {\n      \/\/ body inside for loop\n      sum += i;     \/\/ sum = sum + i\n    }\n       \n    System.out.println(\"Sum = \" + sum);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>Sum = 500500<\/samp><\/pre>\n\n\n\n<p>Here, the value of&nbsp;<var>sum<\/var>&nbsp;is&nbsp;<strong>0<\/strong>&nbsp;initially. Then, the for loop is iterated from&nbsp;<code>i = 1 to 1000<\/code>. In each iteration,&nbsp;<var>i<\/var>is added to&nbsp;<var>sum<\/var>&nbsp;and its value is increased by&nbsp;<strong>1<\/strong>.<\/p>\n\n\n\n<p>When&nbsp;<var>i<\/var>&nbsp;becomes&nbsp;<strong>1001<\/strong>, the test condition is&nbsp;<code>false<\/code>&nbsp;and&nbsp;<var>sum<\/var>&nbsp;will be equal to&nbsp;<code>0 + 1 + 2 + .... + 1000<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The above program to add the sum of natural numbers can also be written as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Program to find the sum of natural numbers from 1 to 1000.\n\nclass Main {\n  public static void main(String&#91;] args) {\n      \n    int sum = 0;\n    int n = 1000;\n\n    \/\/ for loop\n    for (int i = n; i &gt;= 1; --i) {\n      \/\/ body inside for loop\n      sum += i;     \/\/ sum = sum + i\n    }\n       \n    System.out.println(\"Sum = \" + sum);\n  }\n}<\/code><\/pre>\n\n\n\n<p>The output of this program is the same as the&nbsp;<strong>Example 3<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Java for-each Loop<\/h2>\n\n\n\n<p>The Java for loop has an alternative syntax that makes it easy to iterate through\u00a0arrays\u00a0and collections. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ print array elements \n\nclass Main {\n  public static void main(String&#91;] args) {\n      \n    \/\/ create an array\n    int&#91;] numbers = {3, 7, 5, -5};\n    \n    \/\/ iterating through the array \n    for (int number: numbers) {\n       System.out.println(number);\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>3\n7\n5\n-5\n<\/samp><\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<strong>for-each loop<\/strong>&nbsp;to print each element of the&nbsp;<var>numbers<\/var>&nbsp;array one by one.<\/p>\n\n\n\n<p>In the first iteration of the loop,&nbsp;<var>number<\/var>&nbsp;will be 3,&nbsp;<var>number<\/var>&nbsp;will be 7 in second iteration and so on.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"infinite\">Java Infinite for Loop<\/h3>\n\n\n\n<p>If we set the&nbsp;<strong>test expression<\/strong>&nbsp;in such a way that it never evaluates to&nbsp;<code>false<\/code>, the&nbsp;<code>for<\/code>&nbsp;loop will run forever. This is called infinite for loop. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Infinite for Loop\n\nclass Infinite {\n    public static void main(String&#91;] args) {\n      \n        int sum = 0;\n\n        for (int i = 1; i &lt;= 10; --i) {\n            System.out.println(\"Hello\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here, the test expression ,<code>i &lt;= 10<\/code>, is never&nbsp;<code>false<\/code>&nbsp;and&nbsp;<code>Hello<\/code>&nbsp;is printed repeatedly until the memory runs out.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a class=\"\" href=\"https:\/\/www.programiz.com\/java-programming\/for-loop#flowchart\"><\/a><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.programiz.com\/java-programming\/switch-statement\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming. In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1164"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1164"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}