{"id":6497,"date":"2024-04-21T23:31:35","date_gmt":"2024-04-22T06:31:35","guid":{"rendered":"https:\/\/zentut.com\/java-tutorial\/java-do-while-loop\/"},"modified":"2024-04-21T23:32:14","modified_gmt":"2024-04-22T06:32:14","slug":"java-do-while-loop","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-do-while-loop\/","title":{"rendered":"Java do while loop"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Java <code>do while<\/code> loop to execute a block of code repeated as long as a specified condition is true.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Java do while loop<\/h2>\n\n\n\n<p>Java <code>do while<\/code> loop executes a block of code as long as a condition remains true:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">do<\/span> {\n   <span class=\"hljs-comment\">\/\/ Code to be executed<\/span>\n} <span class=\"hljs-keyword\">while<\/span>(condition);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>condition<\/code> is a boolean expression that evaluates to true or false. <\/p>\n\n\n\n<p>Unlike the <a href=\"https:\/\/zentut.com\/java-tutorial\/java-while-loop\/\">while loop<\/a>, the <code>do while<\/code> loop evaluates the <code>condition<\/code> at the end of each iteration. If the <code>condition<\/code> is true, the loop continues. If the condition become <code>false<\/code>, the loop will terminate.<\/p>\n\n\n\n<p>Therefore, inside the loop, you need to make some changes so that the condition will become false at some point. Otherwise, you&#8217;ll have an indefinite loop.<\/p>\n\n\n\n<p>Because the <code>do while<\/code> loop evaluates the <code>condition<\/code> at the end of each iteration, the <code>do while<\/code> loop is also known as a posttest loop. <\/p>\n\n\n\n<p>Also, the <code>do while<\/code> loop <strong>always runs the first iteration<\/strong> whether the <code>condition<\/code> is true or false. It&#8217;s useful in scenarios where you want to execute the first iteration and test the condition. For example, you want to prompt the user for input and continue checking that input until the user issues a quit signal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java do while loop examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>do while<\/code> loops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) A simple do while loop example<\/h3>\n\n\n\n<p>The following program uses a <code>do while<\/code> loop to display integer numbers from 1 to 5:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">0<\/span>;\n        <span class=\"hljs-keyword\">do<\/span> {\n            count++;\n            System.out.println(count);\n        } <span class=\"hljs-keyword\">while<\/span> (count &lt; <span class=\"hljs-number\">5<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-number\">3<\/span>\n<span class=\"hljs-number\">4<\/span>\n<span class=\"hljs-number\">5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, declare a variable <code>count<\/code> and initialize its value to 0.<\/li>\n\n\n\n<li>Second, display the value of the <code>count<\/code> variable as long as the <code>count<\/code> is less than five. In each iteration, increase the value of the <code>count<\/code> variable by one. When the count reaches 5, the loop exits.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using the do while loop to calculate the sum of a sequence of integers<\/h3>\n\n\n\n<p>The following example illustrates how to use the <code>do while<\/code> loop to calculate the sum of all integers from 1 to 100:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        \n        <span class=\"hljs-keyword\">int<\/span> total = <span class=\"hljs-number\">0<\/span>;\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">1<\/span>;\n\n        <span class=\"hljs-keyword\">do<\/span> {\n            total += count;\n            count++;\n        } <span class=\"hljs-keyword\">while<\/span> (count &lt;= <span class=\"hljs-number\">100<\/span>);\n\n        System.out.println(total);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">5050<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare the <code>total<\/code> and <code>count<\/code> variables and initialize their values to 0 and 1 respectively.<\/p>\n\n\n\n<p>Second, add the value of the <code>count<\/code> to the <code>total<\/code> as long as the <code>count<\/code> is less than or equal to 100. Increase the value of the <code>count<\/code> by one in each iteration.<\/p>\n\n\n\n<p>Third, display the <code>total<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Java <code>do while<\/code> loop to execute a block of code repeatedly as long as a condition remains true, and evaluate the condition at the end of each iteration.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Java do while loop to execute a block of code repeated as long as a specified condition is true.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":18,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6497","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/comments?post=6497"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6497\/revisions"}],"predecessor-version":[{"id":6499,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6497\/revisions\/6499"}],"up":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/2190"}],"wp:attachment":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/media?parent=6497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}