{"id":1168,"date":"2022-02-24T19:26:08","date_gmt":"2022-02-24T19:26:08","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1168"},"modified":"2022-02-24T19:26:08","modified_gmt":"2022-02-24T19:26:08","slug":"while-and-do-while-loop","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/while-and-do-while-loop\/","title":{"rendered":"while and do&#8230;while Loop"},"content":{"rendered":"\n<p>In this tutorial, we will learn how to use while and do while loop in Java with the help of examples.<\/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 you can use a loop. It&#8217;s just a simple example; you can achieve much more with loops.<\/p>\n\n\n\n<p>In the previous tutorial, you learned about\u00a0Java for loop. Here, you are going to learn about\u00a0<code>while<\/code>\u00a0and\u00a0<code>do...while<\/code>\u00a0loops.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"while-loop\">Java while loop<\/h2>\n\n\n\n<p>Java&nbsp;<code>while<\/code>&nbsp;loop is used to run a specific code until a certain condition is met. The syntax of the&nbsp;<code>while<\/code>&nbsp;loop is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (testExpression) {\n    \/\/ body of loop\n}<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ol><li>A&nbsp;<code>while<\/code>&nbsp;loop evaluates the&nbsp;<strong>textExpression<\/strong>&nbsp;inside the parenthesis&nbsp;<code>()<\/code>.<\/li><li>If the&nbsp;<strong>textExpression<\/strong>&nbsp;evaluates to&nbsp;<code>true<\/code>, the code inside the&nbsp;<code>while<\/code>&nbsp;loop is executed.<\/li><li>The&nbsp;<strong>textExpression<\/strong>&nbsp;is evaluated again.<\/li><li>This process continues until the&nbsp;<strong>textExpression<\/strong>&nbsp;is&nbsp;<code>false<\/code>.<\/li><li>When the&nbsp;<strong>textExpression<\/strong>&nbsp;evaluates to&nbsp;<code>false<\/code>, the loop stops.<\/li><\/ol>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"flowchart-while\">Flowchart of while loop<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-while-loop.png\" alt=\"Flowchart of while loop in Java\" title=\"Flowchart of while loop in Java\"\/><figcaption>Flowchart of Java while loop<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-while\">Example 1: Display Numbers from 1 to 5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Program to display numbers from 1 to 5\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ declare variables\n    int i = 1, n = 5;\n\n    \/\/ while loop from 1 to 5\n    while(i &lt;= n) {\n      System.out.println(i);\n      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 this program works.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Iteration<\/th><th>Variable<\/th><th>Condition: i &lt;= n<\/th><th>Action<\/th><\/tr><\/thead><tbody><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\">Example 2: Sum of Positive Numbers Only<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java program to find the sum of positive numbers\nimport java.util.Scanner;\n\nclass Main {\n  public static void main(String&#91;] args) {\n      \n    int sum = 0;\n\n    \/\/ create an object of Scanner class\n    Scanner input = new Scanner(System.in);\n\n    \/\/ take integer input from the user\n    System.out.println(\"Enter a number\");\n    int number = input.nextInt();\n\t   \n    \/\/ while loop continues \n    \/\/ until entered number is positive\n    while (number &gt;= 0) {\n      \/\/ add only positive numbers\n      sum += number;\n\n      System.out.println(\"Enter a number\");\n      number = input.nextInt();\n    }\n\t   \n    System.out.println(\"Sum = \" + sum);\n    input.close();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Enter a number\n25\nEnter a number\n9\nEnter a number\n5\nEnter a number\n-3\nSum = 39<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we have used the\u00a0Scanner class\u00a0to take input from the user. Here,\u00a0<code>nextInt()<\/code>\u00a0takes integer input from the user.<\/p>\n\n\n\n<p>The&nbsp;<code>while<\/code>&nbsp;loop continues until the user enters a negative number. During each iteration, the number entered by the user is added to the&nbsp;<code>sum<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>When the user enters a negative number, the loop terminates. Finally, the total sum is displayed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"do-while-loop\">Java do&#8230;while loop<\/h2>\n\n\n\n<p>The&nbsp;<code>do...while<\/code>&nbsp;loop is similar to while loop. However, the body of&nbsp;<code>do...while<\/code>&nbsp;loop is executed once before the test expression is checked. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>do {\n    \/\/ body of loop\n} while(textExpression);<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ol><li>The body of the loop is executed at first. Then the&nbsp;<strong>textExpression<\/strong>&nbsp;is evaluated.<\/li><li>If the&nbsp;<strong>textExpression<\/strong>&nbsp;evaluates to&nbsp;<code>true<\/code>, the body of the loop inside the&nbsp;<code>do<\/code>&nbsp;statement is executed again.<\/li><li>The&nbsp;<strong>textExpression<\/strong>&nbsp;is evaluated once again.<\/li><li>If the&nbsp;<strong>textExpression<\/strong>&nbsp;evaluates to&nbsp;<code>true<\/code>, the body of the loop inside the&nbsp;<code>do<\/code>&nbsp;statement is executed again.<\/li><li>This process continues until the&nbsp;<strong>textExpression<\/strong>&nbsp;evaluates to&nbsp;<code>false<\/code>. Then the loop stops.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"do-while-flowchart\">Flowchart of do&#8230;while loop<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-do-while-loop.png\" alt=\"Flowchart of do...while loop in Java\" title=\"Flowchart of do...while loop in Java\"\/><figcaption>Flowchart of Java do while loop<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Let&#8217;s see the working of&nbsp;<code>do...while<\/code>&nbsp;loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"do-while-example\">Example 3: Display Numbers from 1 to 5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java Program to display numbers from 1 to 5\n\nimport java.util.Scanner;\n\n\/\/ Program to find the sum of natural numbers from 1 to 100.\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    int i = 1, n = 5;\n\n    \/\/ do...while loop from 1 to 5\n    do {\n      System.out.println(i);\n      i++;\n    } while(i &lt;= 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 this program works.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Iteration<\/th><th>Variable<\/th><th>Condition: i &lt;= n<\/th><th>Action<\/th><\/tr><\/thead><tbody><tr><td>&nbsp;<\/td><td><code>i = 1<\/code><br><code>n = 5<\/code><\/td><td>not checked<\/td><td><samp>1<\/samp> is printed.<br><var>i<\/var> is increased to <strong>2<\/strong>.<\/td><\/tr><tr><td>1st<\/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>2nd<\/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>3rd<\/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>4th<\/td><td><code>i = 5<\/code><br><code>n = 5<\/code><\/td><td><code>true<\/code><\/td><td><samp>6<\/samp> is printed.<br><var>i<\/var> is increased to <strong>6<\/strong>.<\/td><\/tr><tr><td>5th<\/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 4: Sum of Positive Numbers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java program to find the sum of positive numbers\nimport java.util.Scanner;\n\nclass Main {\n  public static void main(String&#91;] args) {\n      \n    int sum = 0;\n    int number = 0;\n\n    \/\/ create an object of Scanner class\n    Scanner input = new Scanner(System.in);\n\t   \n    \/\/ do...while loop continues \n    \/\/ until entered number is positive\n    do {\n      \/\/ add only positive numbers\n      sum += number;\n      System.out.println(\"Enter a number\");\n      number = input.nextInt();\n    } while(number &gt;= 0); \n\t   \n    System.out.println(\"Sum = \" + sum);\n    input.close();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output 1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Enter a number\n25\nEnter a number\n9\nEnter a number\n5\nEnter a number\n-3\nSum = 39<\/samp><\/code><\/pre>\n\n\n\n<p>Here, the user enters a positive number, that number is added to the&nbsp;<var>sum<\/var>&nbsp;variable. And this process continues until the number is negative. When the number is negative, the loop terminates and displays the sum without adding the negative number.<\/p>\n\n\n\n<p><strong>Output 2<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Enter a number\n-8\nSum is 0<\/samp><\/code><\/pre>\n\n\n\n<p>Here, the user enters a negative number. The test condition will be&nbsp;<code>false<\/code>&nbsp;but the code inside of the loop executes once.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"infinite-loop\">Infinite while loop<\/h3>\n\n\n\n<p>If&nbsp;<strong>the condition<\/strong>&nbsp;of a loop is always&nbsp;<code>true<\/code>, the loop runs for infinite times (until the memory is full). For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ infinite while loop\nwhile(true){\n    \/\/ body of loop\n}<\/code><\/pre>\n\n\n\n<p>Here is an example of an infinite&nbsp;<code>do...while<\/code>&nbsp;loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ infinite do...while loop\nint count = 1;\ndo {\n   \/\/ body of loop\n} while(count == 1)<\/code><\/pre>\n\n\n\n<p>In the above programs, the&nbsp;<strong>textExpression<\/strong>&nbsp;is always&nbsp;<code>true<\/code>. Hence, the loop body will run for infinite times.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"for-vs-while\">for and while loops<\/h2>\n\n\n\n<p>The&nbsp;<code>for<\/code>&nbsp;loop is used when the number of iterations is known. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (let i = 1; i &lt;=5; ++i) {\n   \/\/ body of loop\n}<\/code><\/pre>\n\n\n\n<p>And&nbsp;<code>while<\/code>&nbsp;and&nbsp;<code>do...while<\/code>&nbsp;loops are generally used when the number of iterations is unknown. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (condition) {\n    \/\/ body of loop\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how to use while and do while loop in Java with the help of examples. In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then you can use a loop. It&#8217;s just a simple example; [&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\/1168"}],"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=1168"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1168\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}