{"id":5209,"date":"2021-03-23T12:29:13","date_gmt":"2021-03-23T12:29:13","guid":{"rendered":"https:\/\/ded9.com\/?p=5209"},"modified":"2026-02-18T11:44:07","modified_gmt":"2026-02-18T11:44:07","slug":"for-while-and-do-while-loops-in-java-in-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/for-while-and-do-while-loops-in-java-in-simple-language\/","title":{"rendered":"For, While and Do-While Loops in Java \u2014 Simple Guide to Repetition Control"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Loops are used in programming to repeat a specific block of code.\u00a0In this tutorial, you will learn to use the while and do\u2026 while <a href=\"https:\/\/www.javatpoint.com\/java-for-loop\" target=\"_blank\" rel=\"noopener\">loops<\/a> in Java <a href=\"https:\/\/ded9.com\/the-best-programming-languages-for-each-field-everything-you-need-to-know-before-starting-work\/\">programming<\/a>.<\/span><span id=\"more-50085\"><\/span><\/p>\n<p>Repeat a loop until a specific condition is met (bet is false).<\/p>\n<p>Rings are what make computers interesting machines. Suppose you have to print a sentence 50 times on the screen. You can do this 50 times using the print Command (without a ring). How do you want to print a sentence a million times? So you have to use the rings.<\/p>\n<p>This is just a straightforward example. Here you will learn to use the while and do\u2026 while loops to write interesting programs.<\/p>\n<h2>While loop in Java<\/h2>\n<p>The structure of the while loop is as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">while (testExpression) {\r\n\r\n\/\/ codes inside body of while loop\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">How does the while loop work?<\/span><\/h2>\n<p>The condition in parentheses is a Boolean expression.<\/p>\n<p>If the condition is actual,<\/p>\n<ul>\n<li>The expressions are evaluated in the loop body.<\/li>\n<li>Then, the condition is re-evaluated.<\/li>\n<\/ul>\n<p>This process continues until the condition is evaluated incorrectly.<\/p>\n<p>If the condition is misjudged,<\/p>\n<ul>\n<li>The loop ends.<\/li>\n<\/ul>\n<h3><span style=\"font-size: 14pt;\">While loop flowchart<\/span><\/h3>\n<p><strong>Example 1: while loop<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ Program to print line 10 times<\/li>\n<li dir=\"ltr\">class Loop {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int i = 1;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">while (i &lt;= 10) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cLine\u201d + i);<\/li>\n<li dir=\"ltr\">++ i;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Line 1<\/p>\n<p dir=\"ltr\">Line 2<\/p>\n<p dir=\"ltr\">Line 3<\/p>\n<p dir=\"ltr\">Line 4<\/p>\n<p dir=\"ltr\">Line 5<\/p>\n<p dir=\"ltr\">Line 6<\/p>\n<p dir=\"ltr\">Line 7<\/p>\n<p dir=\"ltr\">Line 8<\/p>\n<p dir=\"ltr\">Line 9<\/p>\n<p dir=\"ltr\">Line 10<\/p>\n<\/blockquote>\n<p>Note: i inside the loop will equal 11 after 10 repetitions. Then, the condition i &lt;= 10 is considered false, and the while loop ends.<\/p>\n<p><strong>Example 2: while loop<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ Program to find the sum of natural numbers from 1 to 100.<\/li>\n<li dir=\"ltr\">class AssignmentOperator {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int sum = 0, i = 100;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">while (i! = 0) {<\/li>\n<li dir=\"ltr\">sum + = i;\u00a0\/\/ sum = sum + i;<\/li>\n<li dir=\"ltr\">\u2013I;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">System.out.println (\u201cSum =\u201d + sum);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Sum = 5050<\/p>\n<\/blockquote>\n<p>Here, the value of the sum variable is 0, and the initial value of i is 100. In each iteration of the loop, the variable&#8217;s value is summed with i (sum + i), and&#8217;s valuef i decreases by one until i equals 0. For better visualization,<\/p>\n<blockquote>\n<p dir=\"ltr\">1st iteration: sum = 0 + 100 = 100, i = 99<\/p>\n<p dir=\"ltr\">2nd iteration: sum = 100 + 99 = 199, i = 98<\/p>\n<p dir=\"ltr\">3rd iteration: sum = 199 + 98 = 297, i = 97<\/p>\n<p dir=\"ltr\">\u2026 ..\u2026<\/p>\n<p dir=\"ltr\">\u2026 ..\u2026<\/p>\n<p dir=\"ltr\">99th iteration: sum = 5047 + 2 = 5049, i = 1<\/p>\n<p dir=\"ltr\">100th iteration: sum = 5049 + 1 = 5050, i = 0<\/p>\n<\/blockquote>\n<h1><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258182 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Do-while-loop-in-java-1.png\" alt=\"How does the while loop work?\" width=\"835\" height=\"446\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Do-while-loop-in-java-1.png 835w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Do-while-loop-in-java-1-300x160.png 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Do-while-loop-in-java-1-768x410.png 768w\" sizes=\"(max-width: 835px) 100vw, 835px\" \/><\/span><\/h1>\n<h2><span style=\"font-size: 14pt;\">Do\u2026 while loop in Java.<\/span><\/h2>\n<p>The do\u2026 while loop is similar to the while loop with one key difference.\u00a0The body of the do\u2026 while loop is executed once before the condition is checked.<\/p>\n<p>Do\u2026 while loop structure:<\/p>\n<blockquote>\n<p dir=\"ltr\">do {<\/p>\n<p dir=\"ltr\">\/\/ codes inside body of do while loop<\/p>\n<p dir=\"ltr\">} while (testExpression);<\/p>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\">How do the do\u2026 while loops work?<\/span><\/h2>\n<p>The body of the do\u2026 while loop is executed once (before checking the condition). The condition is checked in the next series.<\/p>\n<p>If the condition is evaluated correctly, the code inside the loop&#8217;s body is executed, and the condition is re-evaluated. This process continues until the condition is considered incorrect.<\/p>\n<p>When the condition is false, the do\u2026 while loop ends.<\/p>\n<h3><span style=\"font-size: 14pt;\">Do\u2026 while loop flowchart.<\/span><\/h3>\n<h1><span style=\"font-size: 18pt;\"><img decoding=\"async\" class=\"alignnone wp-image-258179 size-full\" title=\"loops\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/do-while-loop.webp\" alt=\"Do\u2026 while loop flowchart.\" width=\"1503\" height=\"1503\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/do-while-loop.webp 1503w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/do-while-loop-300x300.webp 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/do-while-loop-1024x1024.webp 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/do-while-loop-150x150.webp 150w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/do-while-loop-768x768.webp 768w\" sizes=\"(max-width: 1503px) 100vw, 1503px\" \/><\/span><\/h1>\n<p><strong>Example 3: do\u2026 while loop<\/strong><\/p>\n<p>The following program calculates the numbers the user enters until login 0.<\/p>\n<p>We used the Scanner object to get input from the user.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">import java.util.Scanner;<\/li>\n<li dir=\"ltr\">class Sum {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">Double number, sum = 0.0;<\/li>\n<li dir=\"ltr\">Scanner input = new Scanner (System.in);<\/li>\n<li dir=\"ltr\">do {<\/li>\n<li dir=\"ltr\">System.out.print (&#8220;Enter a number:&#8221;);<\/li>\n<li dir=\"ltr\">number = input.nextDouble ();<\/li>\n<li dir=\"ltr\">sum + = number;<\/li>\n<li dir=\"ltr\">} while (number! = 0.0);<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">System.out.println (\u201cSum =\u201d + sum);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Enter a number: 2.5<\/p>\n<p dir=\"ltr\">Enter a number: 23.3<\/p>\n<p dir=\"ltr\">Enter a number: -4.2<\/p>\n<p dir=\"ltr\">Enter a number: 3.4<\/p>\n<p dir=\"ltr\">Enter a number: 0<\/p>\n<p dir=\"ltr\">Sum = 25.0<\/p>\n<\/blockquote>\n<h3><span style=\"font-size: 14pt;\">Infinite while loop<\/span><\/h3>\n<p>The loop&#8217;s body is done indefinitely if the condition is never wrong. The while loop is executed many times (at least in theory). For example<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">while (true) {<\/li>\n<li dir=\"ltr\">\/\/ body of while loop<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Another example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">int i = 100;<\/li>\n<li dir=\"ltr\">while (i == 100) {<\/li>\n<li dir=\"ltr\">System.out.print (\u201cHey!\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is a for loop used for in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A for loop is ideal when you know in advance how many times you want to repeat a block of code \u2014 it sets up initialization, condition, and update in one statement.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">When should you use a while loop?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use a while loop when the number of iterations depends on a condition that may change during runtime \u2014 it repeatedly runs as long as the condition remains true.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What makes do-while different from while?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>In a do-while loop, the code block runs at least once before the condition is checked \u2014 so even if the condition is false at the beginning, the loop body still executes once.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Loops are used in programming to repeat a specific block of code.\u00a0In this tutorial, you will learn to use the while and do\u2026 while loops in Java programming. Repeat a loop until a specific condition is met (bet is false). Rings are what make computers interesting machines. Suppose you have to print a sentence 50 [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5210,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5209","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5209"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5209\/revisions"}],"predecessor-version":[{"id":267021,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5209\/revisions\/267021"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5210"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}