{"id":5215,"date":"2021-03-23T12:39:15","date_gmt":"2021-03-23T12:39:15","guid":{"rendered":"https:\/\/ded9.com\/?p=5215"},"modified":"2025-12-21T11:01:19","modified_gmt":"2025-12-21T11:01:19","slug":"learn-loops-for-in-java-in-quite-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/learn-loops-for-in-java-in-quite-simple-language\/","title":{"rendered":"Learn Loops for-In in Java in Simple Language"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Loops are used in programming to repeat a specific block of code. In this tutorial, you can create a for loop in Java programming. <\/span><span style=\"font-size: 12pt;\">Loops are used in programming to repeat a block of code until a condition is met (i.e., the condition is false).<\/span><span id=\"more-50077\"><\/span><\/p>\n<p>Rings are what make computers interesting machines. Imagine, for a moment, that you were transposed into Earl&#8217;s karmically driven world. You can do this using the print Command 50 times (without a loop).<\/p>\n<p>How do you want to print a sentence a million times?\u00a0So you have to use the rings.<\/p>\n<div>\n<h2 class=\"break-words\" dir=\"auto\"><strong>What Are Loops in Java?<\/strong><\/h2>\n<p class=\"break-words\" dir=\"auto\">Loops in Java are a fundamental feature that allows a section of a program to be executed repeatedly while a given condition remains true. While the core purpose of all loop types is similar, they differ significantly in syntax and usage.<\/p>\n<h3 class=\"break-words\" dir=\"auto\"><strong>For Loops in Java<\/strong><\/h3>\n<p class=\"break-words\" dir=\"auto\">The <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for<\/span> loop in Java is designed to iterate over a block of code a predetermined number of times, making it ideal for tasks like processing arrays or performing calculations. Mastering <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for<\/span> loops is essential for efficient iteration and data handling. To dive deeper into <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for<\/span> loops and other Java programming constructs, consider enrolling in a Java course.<\/p>\n<h3 class=\"break-words\" dir=\"auto\"><strong>Comparing For, While, and Do-While Loops<\/strong><\/h3>\n<p class=\"break-words\" dir=\"auto\">Java offers three main types of loops\u2014<span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for<\/span>, <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">while<\/span>, and <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">do-while<\/span>\u2014each with distinct characteristics in terms of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Syntax\" target=\"_blank\" rel=\"noopener\">syntax<\/a>, condition checking, and optimal use cases. Below is a breakdown of their differences:<\/p>\n<div class=\"overflow-x-auto my-2\" dir=\"auto\">\n<table dir=\"auto\">\n<thead class=\"border-b border-primary\/20\">\n<tr class=\"border-primary\/10\">\n<th class=\"break-words\"><strong>Aspect<\/strong><\/th>\n<th class=\"break-words\"><strong>For Loop<\/strong><\/th>\n<th class=\"break-words\"><strong>While Loop<\/strong><\/th>\n<th class=\"break-words\"><strong>Do-While Loop<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"border-primary\/10\">\n<td class=\"break-words\"><strong>Introduction<\/strong><\/td>\n<td class=\"break-words\">Iterates a set of statements a specific number of times.<\/td>\n<td class=\"break-words\">Executes statements until a Boolean condition becomes false.<\/td>\n<td class=\"break-words\">Runs statements at least once, then repeats until a boolean condition is false.<\/td>\n<\/tr>\n<tr class=\"border-primary\/10\">\n<td class=\"break-words\"><strong>Best Use Case<\/strong><\/td>\n<td class=\"break-words\">Use when the exact number of iterations is known (e.g., array processing).<\/td>\n<td class=\"break-words\">Use when the number of iterations is unknown.<\/td>\n<td class=\"break-words\">Use when the loop must execute at least once, even if the condition is unknown.<\/td>\n<\/tr>\n<tr class=\"border-primary\/10\">\n<td class=\"break-words\"><strong>Syntax<\/strong><\/td>\n<td class=\"break-words\"><span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for (init; condition; icr\/dcr) { \/\/ statements }<\/span><\/td>\n<td class=\"break-words\"><span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">while (condition) { \/\/ statements }<\/span><\/td>\n<td class=\"break-words\"><span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">do { \/\/ statements } while (condition);<\/span><\/td>\n<\/tr>\n<tr class=\"border-primary\/10\">\n<td class=\"break-words\"><strong>Example<\/strong><\/td>\n<td class=\"break-words\"><span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for (int x = 0; x &lt;= 5; x++) { System.out.println(x); }<\/span><\/td>\n<td class=\"break-words\"><span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">int x = 0; while (x &lt;= 5) { System.out.println(x); x++; }<\/span><\/td>\n<td class=\"break-words\"><span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">int x = 0; do { System.out.println(x); x++; } while (x &lt;= 5);<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"break-words\" dir=\"auto\">This article will examine the for loop in Java in greater detail, but understanding the distinctions among these loop types is crucial for selecting the appropriate one for your programming needs.<\/p>\n<div>\n<h3 class=\"break-words\" dir=\"auto\"><strong>Overview of For Loops<\/strong><\/h3>\n<p class=\"break-words\" dir=\"auto\">A <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for<\/span> loop in Java repeatedly executes a block of code a fixed number of times, making it ideal when the number of iterations is known in advance. Let&#8217;s break down its syntax and functionality to understand how it works.<\/p>\n<h3 class=\"break-words\" dir=\"auto\"><strong>Syntax of a For Loop<\/strong><\/h3>\n<div class=\"not-prose\" dir=\"auto\">\n<div class=\"relative [&amp;_div+div]:!mt-0 mt-3 mb-3 -mx-4 -mr-2 @md:-mr-4\">\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;}\">for (init; condition; incr\/decr) { \/\/ statements to be executed or body }<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<ul class=\"marker:text-secondary\" dir=\"auto\">\n<li class=\"break-words\"><strong>init<\/strong>: This expression initializes a variable (e.g., <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">int x = 0<\/span>) and is executed only once at the start of the loop.<\/li>\n<li class=\"break-words\"><strong>condition<\/strong>: This boolean expression (e.g., <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">x &lt;= 5<\/span>) is evaluated before each iteration. If the condition is true, the loop body executes; otherwise, the loop terminates.<\/li>\n<li class=\"break-words\"><strong>incr\/decr<\/strong>: This statement (e.g., <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">x++<\/span>) updates the initialized variable after each iteration to eventually make the condition false.<\/li>\n<\/ul>\n<h3 class=\"break-words\" dir=\"auto\"><strong>Flow of a For Loop<\/strong><\/h3>\n<p class=\"break-words\" dir=\"auto\"><strong><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258666 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/flowofforloop.jpg\" alt=\"Flow of a For Loop\" width=\"782\" height=\"1086\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/flowofforloop.jpg 782w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/flowofforloop-216x300.jpg 216w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/flowofforloop-737x1024.jpg 737w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/flowofforloop-768x1067.jpg 768w\" sizes=\"(max-width: 782px) 100vw, 782px\" \/><\/strong><\/p>\n<ol class=\"marker:text-secondary\" dir=\"auto\">\n<li class=\"break-words\">The <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">init<\/span> expression is executed once to set the starting value.<\/li>\n<li class=\"break-words\">The <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">condition<\/span> is checked; if it is true, the loop body executes.<\/li>\n<li class=\"break-words\">After each iteration, the <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">incr\/decr<\/span> statement updates the variable.<\/li>\n<li class=\"break-words\">The <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">condition<\/span> is re-evaluated, and the loop continues until the condition becomes false.<\/li>\n<\/ol>\n<h3 class=\"break-words\" dir=\"auto\"><strong>Example of a For Loop<\/strong><\/h3>\n<p class=\"break-words\" dir=\"auto\">Here&#8217;s a simple example to illustrate how a <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">for<\/span> loop operates in <a href=\"https:\/\/ded9.com\/what-is-object-orientation-in-java-and-how-to-use-it\/\">Java<\/a>:<\/p>\n<div class=\"not-prose\" dir=\"auto\">\n<div class=\"relative [&amp;_div+div]:!mt-0 mt-3 mb-3 -mx-4 -mr-2 @md:-mr-4\">\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;}\">public class ForExample { public static void main(String[] args) { for (int x = 0; x &lt;= 5; x++) { System.out.println(x); } } }<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"break-words\" dir=\"auto\"><strong>Output<\/strong><\/p>\n<p class=\"break-words\" dir=\"auto\"><strong><img decoding=\"async\" class=\"aligncenter wp-image-258669 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/ForLoop_1.jpg\" alt=\"Here's a simple example to illustrate how a for loop operates in Java:\" width=\"1231\" height=\"350\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/ForLoop_1.jpg 1231w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/ForLoop_1-300x85.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/ForLoop_1-1024x291.jpg 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/ForLoop_1-768x218.jpg 768w\" sizes=\"(max-width: 1231px) 100vw, 1231px\" \/><\/strong><\/p>\n<p class=\"break-words\" dir=\"auto\">In this example, the loop starts with <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">x = 0<\/span>, checks whether x &lt;= 5, prints <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">x<\/span>, and increments <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">x<\/span> by 1 in each iteration until the condition becomes false.<\/p>\n<\/div>\n<\/div>\n<h3><span style=\"font-size: 14pt;\">Ring for<\/span><\/h3>\n<p>The loop structure in Java 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;}\">for (initialization; testExpression; update)\r\n\r\n{\r\n\r\n\/\/ codes inside for loop's body\r\n\r\n}<\/pre>\n<\/div>\n<h2>How does the for loop work?<\/h2>\n<p>1- The initialization is performed only once.<\/p>\n<p>2- Then the condition is evaluated as a Boolean expression.<\/p>\n<p>3- If the condition is evaluated correctly,<\/p>\n<ul>\n<li>The code within the body of the loop is executed.<\/li>\n<li>The Update statement is then executed.<\/li>\n<li>Again, the condition is evaluated.<\/li>\n<li>If the condition is satisfied, the code in the loop body and the Update statement are executed.<\/li>\n<li>This process continues until the condition is evaluated incorrectly.<\/li>\n<\/ul>\n<p>4- If the condition is evaluated incorrectly, the for loop ends.<\/p>\n<h1><span style=\"font-size: 18pt;\">Ring flowchart for<\/span><\/h1>\n<h1><span style=\"font-size: 18pt;\"><img decoding=\"async\" class=\"aligncenter wp-image-258140 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/7nph3w8nu4981.webp\" alt=\"Ring flowchart for\" width=\"831\" height=\"1033\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/7nph3w8nu4981.webp 831w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/7nph3w8nu4981-241x300.webp 241w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/7nph3w8nu4981-824x1024.webp 824w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/7nph3w8nu4981-768x955.webp 768w\" sizes=\"(max-width: 831px) 100vw, 831px\" \/><\/span><\/h1>\n<p><strong>Example 1: ring for<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ Program to print a sentence 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\">for (int i = 1; i &lt;= 10; ++ i) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cLine\u201d + 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>Here, the variable i is defined and assigned the value 1.<\/p>\n<p>The condition i = 10 is then evaluated. The loop&#8217;s body runs right, which prints Line 1 on the screen.<\/p>\n<p>Then the expression ++i is evaluated. The value of i has now been increased to 2. Again, the condition i = 10 is assessed as accurate, and the loop body executes, printing Line 2 to the screen.<\/p>\n<p>This iteration process continues until i = 11. When i equals 11, the condition i &lt;= 10 is false, and the loop ends.<\/p>\n<p><strong>Example 2: ring for<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ Program to find the sum of natural numbers from 1 to 1000.<\/li>\n<li dir=\"ltr\">class Number {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int sum = 0;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">for (int i = 1; i &lt;= 1000; ++ i) {<\/li>\n<li dir=\"ltr\">sum + = i;\u00a0\/\/ sum = sum + i<\/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 = 500500<\/p>\n<\/blockquote>\n<p>Here, the value of the sum variable starts at 0. Each time the loop iterates, the sum variable is updated to sum + i, and i increases until it exceeds 1000.<\/p>\n<blockquote>\n<p dir=\"ltr\">1st iteration: sum = 0 + 1 = 1<\/p>\n<p dir=\"ltr\">2nd iteration: sum = 1 + 2 = 3<\/p>\n<p dir=\"ltr\">3rd iteration: sum = 3 + 3 = 6<\/p>\n<p dir=\"ltr\">4th iteration: sum = 6 + 4 = 10<\/p>\n<p dir=\"ltr\">\u2026 ..\u2026<\/p>\n<p dir=\"ltr\">999th iteration: sum = 498501 + 999 = 499500<\/p>\n<p dir=\"ltr\">1000th iteration: sum = 499500 + 1000 = 500500<\/p>\n<\/blockquote>\n<h3 id=\"for-loop\"><span style=\"font-size: 14pt;\">Java for Loop<\/span><\/h3>\n<p>Java\u00a0<code>for<\/code> A loop executes a block of code a specified number of times. The syntax of <code>for<\/code>\u00a0loop is:<\/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;}\">for (initialExpression; testExpression; updateExpression) { \/\/ body of the loop }<\/pre>\n<\/div>\n<p>Here,<\/p>\n<ol>\n<li>The\u00a0<var>initialExpression<\/var>\u00a0initializes and\/or declares\u00a0variables\u00a0and executes only once.<\/li>\n<li>It <code>condition<\/code> is evaluated. If it\u00a0<code>condition<\/code>\u00a0is\u00a0<code>true<\/code>, the body of the\u00a0<code>for<\/code>\u00a0loop is executed.<\/li>\n<li>The\u00a0<code>updateExpression<\/code>\u00a0updates the value of\u00a0<code>initialExpression<\/code>.<\/li>\n<\/ol>\n<ol>\n<li>The\u00a0<strong>condition<\/strong> is re-evaluated. The process continues until the\u00a0<strong>condition<\/strong>\u00a0is\u00a0<code>false<\/code>.<\/li>\n<\/ol>\n<h2><span style=\"font-size: 18pt;\">Ring for infinity<\/span><\/h2>\n<h1><span style=\"font-size: 18pt;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-258143 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/infinite-loop-in-java-thumbnail.webp\" alt=\"Ring for infinity\" width=\"867\" height=\"801\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/infinite-loop-in-java-thumbnail.webp 867w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/infinite-loop-in-java-thumbnail-300x277.webp 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/infinite-loop-in-java-thumbnail-768x710.webp 768w\" sizes=\"(max-width: 867px) 100vw, 867px\" \/><\/span><\/h1>\n<p>If the condition is always true, the loop will execute forever; such a loop is called an infinite loop. For example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ Infinite for Loop<\/li>\n<li dir=\"ltr\">class Infinite {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int sum = 0;<\/li>\n<li dir=\"ltr\">for (int i = 1; i &lt;= 10; \u2013i) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cHello\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Here, the condition i &lt;= 10 is never false, and Hello is printed many times (at least in theory).<\/p>\n<p>The initialization of the variable, the variable update, and the condition used in the for loop are optional.\u00a0Here is another example of an infinite loop:<\/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;}\">for (;;) {\r\n}<\/pre>\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-in loop in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It is an enhanced for loop used to iterate through arrays and collections easily.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you write a for-in loop in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the syntax for (Type item : collection) { \u2026 } to process elements sequentially.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why use a for-in loop?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It simplifies iteration and reduces errors compared to traditional for loops.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Loops are used in programming to repeat a specific block of code. In this tutorial, you can create a for loop in Java programming. Loops are used in programming to repeat a block of code until a condition is met (i.e., the condition is false). Rings are what make computers interesting machines. Imagine, for a [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5216,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5215","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\/5215","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=5215"}],"version-history":[{"count":11,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5215\/revisions"}],"predecessor-version":[{"id":266367,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5215\/revisions\/266367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5216"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}