{"id":6494,"date":"2024-04-21T23:30:15","date_gmt":"2024-04-22T06:30:15","guid":{"rendered":"https:\/\/zentut.com\/java-tutorial\/java-while-loop\/"},"modified":"2024-04-21T23:31:01","modified_gmt":"2024-04-22T06:31:01","slug":"java-while-loop","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-while-loop\/","title":{"rendered":"Java while loop"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you will learn how to use the Java <code>while<\/code> loop statement to execute a block of code repeatedly as long as a specified condition is true.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Java while loop<\/h2>\n\n\n\n<p>A <code>while<\/code> loop allows you to execute a block of code repeatedly as long as a certain condition remains true.  <\/p>\n\n\n\n<p>Here&#8217;s the basic syntax of the <code>while<\/code> loop:<\/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\">while<\/span>(condition) {\n    <span class=\"hljs-comment\">\/\/ Code to be executed as long as the condition is true<\/span>\n}<\/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 determines whether the loop should continue or terminate.<\/p>\n\n\n\n<p>Java evaluates the <code>condition<\/code> before each iteration. If the <code>condition<\/code> is true, the loop continues, otherwise, the loop exits.<\/p>\n\n\n\n<p>Because the <code>while<\/code> loop statement evaluates the condition at the beginning of each iteration, the <code>while<\/code> loop is also known as a pretest loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java while loop examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>while<\/code> loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using a while loop to display numbers from 0 to 4<\/h3>\n\n\n\n<p>The following example uses the <code>while<\/code> loop to print out numbers from 0 to 4:<\/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\">while<\/span>(count &lt; <span class=\"hljs-number\">5<\/span>){\n            System.out.println(count);\n            count++;\n        }\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\">0<\/span>\n<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><\/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<p>First, declare a <a href=\"https:\/\/zentut.com\/java-tutorial\/java-variables\/\">variable<\/a> <code>count<\/code> and initialize its value to zero.<\/p>\n\n\n\n<p>Second, use the <code>while<\/code> loop to print the value of the <code>count<\/code> variable as long as the <code>count<\/code> is less than 5. <\/p>\n\n\n\n<p>Inside the loop, we increment the <code>count<\/code> variable by one in each iteration using <code>count++<\/code>. The loop will continue until the condition <code>count &lt; 5<\/code> becomes <code>false<\/code>. Once the count is 5, the loop exits. <\/p>\n\n\n\n<p class=\"note\">Notice that if you forget to increase the <code>count<\/code> inside the loop, the condition <code>count &lt; 5<\/code> is always true. As a result, the loop will run forever. In this case, you&#8217;ll have an <strong>indefinite loop<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using a while loop to calculate the sum of numbers<\/h3>\n\n\n\n<p>The following program uses a <code>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        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">1<\/span>, total = <span class=\"hljs-number\">0<\/span>;\n\n        <span class=\"hljs-keyword\">while<\/span>(count &lt;= <span class=\"hljs-number\">100<\/span>){\n            total += count;\n            count++;\n        }\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<ul class=\"wp-block-list\">\n<li>First, declare the <code>count<\/code> variable and initialize its value to 1. Also, declare the <code>total<\/code> variable with the initial value of 0.<\/li>\n\n\n\n<li>Second, use a <code>while<\/code> loop that adds up the <code>count<\/code> to the <code>total<\/code> as long as the <code>count<\/code> is less than or equal to 100. <\/li>\n\n\n\n<li>Third, display the <code>total<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>Notice that this example is for demonstration purposes. If you know Math, you can calculate the total of all integers from 1 to n using the formula: <code>n * (n + 1) \/ 2<\/code>. <\/p>\n\n\n\n<p>Here&#8217;s the program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" 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> n = <span class=\"hljs-number\">100<\/span>;\n        <span class=\"hljs-keyword\">int<\/span> total = n * (n + <span class=\"hljs-number\">1<\/span>) \/ <span class=\"hljs-number\">2<\/span>;\n        System.out.println(total);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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-7\" 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-7\"><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<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a Java <code>while<\/code> loop to execute a block of code as long as a condition is <code>true<\/code>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Java while loop statement to execute a block of code repeatedly as long as a specified condition is true.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6494","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6494","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=6494"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6494\/revisions"}],"predecessor-version":[{"id":6496,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6494\/revisions\/6496"}],"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=6494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}