{"id":76370,"date":"2020-02-13T09:51:54","date_gmt":"2020-02-13T04:21:54","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=76370"},"modified":"2020-02-13T09:51:54","modified_gmt":"2020-02-13T04:21:54","slug":"java-loops","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-loops\/","title":{"rendered":"Java Loops &#8211; A Complete Guide for Beginners!"},"content":{"rendered":"<p>Have you ever forgot to do your homework and as a punishment you were asked to write <em>&#8220;I will do my homework on time.&#8221;<\/em> for at least 40-50 times? It was boring as well as time-consuming, right? Well, Java Loops works exactly the same.<\/p>\n<p>Loops in programming allow a set of instructions to be executed repeatedly until a certain condition is fulfilled. Loops are also known as iterating statements or looping statements. In this article, we will learn about the various loops in Java.<\/p>\n<h3>Need for Loops in Java<\/h3>\n<p>While programming, sometimes, there occurs a situation when we need to execute a block of code several numbers of times. In general, these statements execute in a sequential manner: The first statement in a function executes first, followed by the second, and so on.<\/p>\n<p>But this makes the process very complicated as well as lengthy and therefore time-consuming. Therefore, programming languages provide various control structures that allow for such complex execution statements.<\/p>\n<p>Before moving towards the types of loops, we will first discuss the general syntax of a loop with the help of elements that control a loop.<\/p>\n<h3>Java Loops<\/h3>\n<p>In Java, there are three kinds of loops which are &#8211; the <strong>for<\/strong> loop, the <strong>while<\/strong> loop, and the <strong>do-while<\/strong> loop. All these three loop constructs of Java executes a set of repeated statements as long as a specified condition remains true.<\/p>\n<p>This particular condition is generally known as loop control. For all three loop statements, a true condition is the one that returns a boolean true value and the false condition is the one that returns the boolean false value.<\/p>\n<h3>Elements in a Java Loop<\/h3>\n<p>Every loop has its elements or <em><strong>variables<\/strong><\/em> that govern its execution. Generally, a loop has four elements that have different purposes which are:<\/p>\n<ul>\n<li>Initialization Expression(s)<\/li>\n<li>Test Expression(Condition)<\/li>\n<li>Update Expression(s)<\/li>\n<li>Body of the loop<\/li>\n<\/ul>\n<p>We will discuss each of the above elements for a better understanding of the working of the loops.<\/p>\n<h4>1. Initialization Expression(s)<\/h4>\n<p>Before entering into a loop, we must initialize its control variable. The initialization of the control variable takes place under initialization expression. It initializes the loop variable(s) with their first value. The initialization expression gets executed only once at the beginning of the loop.<\/p>\n<h4>2. Test Expression<\/h4>\n<p>The test expression is an expression whose truth (boolean) value decides whether the loop body will be executed or not. The execution or termination of the loop depends on the test expression which is also called the exit condition or test condition.<\/p>\n<p>If the test expression evaluates to true that is, 1, the loop body is executed, otherwise, the loop is terminated.<\/p>\n<p>In an <strong>entry-controlled loop<\/strong>, the test expression is evaluated before entering into a loop whereas, in <strong>the exit-controlled loop<\/strong>, the test expression is evaluated before exiting from the loop. In Java, the <strong>for<\/strong> loop and <strong>while<\/strong> loop are entry-controlled loops, and <strong>do-while<\/strong> loop is an <strong>exit-controlled<\/strong> loop.<\/p>\n<h4>3. Update Expression(s)<\/h4>\n<p>The update expression(s) changes the values of the loop variables. The update expression is executed at the end of the loop after the loop body gets executed. For example, an update expression may be increment or decrement statements.<\/p>\n<h4>4. The Body of the Loop<\/h4>\n<p>The statements which execute repeatedly (as long as the test expression is non zero) form the body of the loop. The code inside the loop body will be executed or not, depends on the value of the test expression.<\/p>\n<p>If the value evaluates to be true then the loop body gets repeatedly executed, otherwise, it gets terminated.<\/p>\n<p>Following diagram explains an Iteration or a loop construct:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-loop-constructor.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-76530\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-loop-constructor.jpg\" alt=\"loop constructor\" width=\"548\" height=\"436\" \/><\/a><\/p>\n<h3>Types of Loops in Java<\/h3>\n<h4>1. The for Loop<\/h4>\n<p>The <strong>for<\/strong> loop in Java is an entry controlled loop that allows a user to execute a block of a statement(s) repeatedly with a fixed number of times on the basis of the test expression or test-condition. This\u00a0is the easiest to understand Java loops.<\/p>\n<p>All its loop-control elements are gathered at one place, on the top of the loop within the round brackets(), while in the other loop constructions of Java, the loop elements are scattered about the program.<\/p>\n<p><strong>The syntax or general form of for loop is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for(initialization expression(s) ; test-expression ; update-expression(s))\n{\n     body of the loop ;\n}<\/pre>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int x = 0;\nfor( x = 1 ; x &lt;= 10 ; x++ )\n{\n  System.out.println(Value of x: \u201c +x);\n}<\/pre>\n<p><strong>Code Snippet to illustrate the use of for statement\/loop:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.TechVidvan.loopsDemo;\npublic class ForLoopDemo\n{\n  public static void main(String args[])\n  {\n    int i;\n    for(i = 10; i &gt;= 1; i--)\n    {\n      System.out.println(\"The value of i is: \"+i);\n    }\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The value of i is: 10<br \/>\nThe value of i is: 9<br \/>\nThe value of i is: 8<br \/>\nThe value of i is: 7<br \/>\nThe value of i is: 6<br \/>\nThe value of i is: 5<br \/>\nThe value of i is: 4<br \/>\nThe value of i is: 3<br \/>\nThe value of i is: 2<br \/>\nThe value of i is: 1<\/div>\n<p><em>The following figure outlines the working of a for loop:<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-for-loop-in-java-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-76535\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-for-loop-in-java-1.jpg\" alt=\"for loop in Java\" width=\"566\" height=\"576\" \/><\/a><\/p>\n<p>Now that you are familiar with the working of a for loop, let us take another example where there are <em><strong>multiple statements in the loop body:<\/strong><\/em><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.TechVidvan.loopsDemo;\n\npublic class ForLoopDemo\n{\n  public static void main(String args[])\n  {\n    int i,sum;\n    for(i = 1 , sum = 0; i &lt;= 10; ++i)\n    {\n      System.out.println(\"The value of i is: \"+i) ;\n      sum = sum + i;\n    }\n    System.out.println(\"The sum of first 10 numbers is: \" +sum) ;\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The value of i is: 1<br \/>\nThe value of i is: 2<br \/>\nThe value of i is: 3<br \/>\nThe value of i is: 4<br \/>\nThe value of i is: 5<br \/>\nThe value of i is: 6<br \/>\nThe value of i is: 7<br \/>\nThe value of i is: 8<br \/>\nThe value of i is: 9<br \/>\nThe value of i is: 10<br \/>\nThe sum of first 10 numbers is: 55<\/div>\n<p>In the above program, there are 2 initialization expressions: i = 1 and sum = 0 separated by comma. The initialization part may contain as many expressions but these should be separated by commas. The initialization part must be followed by a semicolon(;). Both the variables <strong>i<\/strong> and <strong>sum<\/strong> get their first values 1 and 0 respectively.<\/p>\n<p><strong>Tip:<\/strong> Use for loop when you have to repeat a block of statements a specific number of times.<\/p>\n<h5>The for loop Variations<\/h5>\n<p>Java offers several variations in the loop that increases the flexibility and applicability of <strong>for<\/strong> loop. The different variations of for loop are discussed below:<\/p>\n<p><strong>1.1. Multiple Initializations and Update Expressions<\/strong><\/p>\n<p>A for loop may contain multiple initializations and\/or update expressions. These multiple expressions must be separated by commas. We have already seen an example of multiple initialization expressions in the previous program.<\/p>\n<p>The for loop of that program can be alternatively written as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for( i = 1, sum = 0 ; i &lt;= 10 ; sum +=i, ++i )\nSystem.out.println(i);<\/pre>\n<p>The above code contains two initialization expressions <strong>i = 1 and sum = 0<\/strong> and two update expressions <strong>sum += i<\/strong> and <strong>++i<\/strong>. These multiple expressions are executed in sequence.<\/p>\n<p><strong>Tip:<\/strong> The comma operator in a for loop is essential whenever we need more than one index.<\/p>\n<p><strong>1.2. Optional Expressions<\/strong><\/p>\n<p>In a for loop, initialization expressions, test expressions and, update expressions are optional that is, you can skip any or all of these expressions.<\/p>\n<p>Say, for example, you have already initialized the loop variables and you want to scrape off the initialization expression then you can write for loop as follows:<\/p>\n<p>for( ; test-expression ; update-expression(s))<br \/>\nloop-body<\/p>\n<p>See, even if you skip the initialization expression, the semicolon (;) must be following it.<\/p>\n<p>Following code fragment illustrates the above concept:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.TechVidvan.loopsDemo;\n\npublic class ForLoopDemo\n{\n  public static void main(String args[])\n  {\n    int i = 1, sum = 0 ;\n    for( ; i &lt;= 10 ; sum +=i, ++i )\n      System.out.println(i);\n    System.out.println(\"The sum of first 10 numbers is: \" +sum) ;\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n8<br \/>\n9<br \/>\n10<br \/>\nThe sum of first 10 numbers is: 55<\/div>\n<p>Similarly, we can also skip or omit the test expressions and update expressions.<\/p>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for( j = 0 ; j != 224 ; )\nj += 11 ;<\/pre>\n<p>If the variable j has already been initialized, then we can write the above loop as,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for( ; j != 224 ; )\nj += 11;<\/pre>\n<p><strong>Tip:<\/strong> The loop-control expressions in a for loop statement are optional, but semicolons must be written.<\/p>\n<p><strong>1.3. Infinite Loop<\/strong><\/p>\n<p>An infinite loop can be created by skipping the test-expression as shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.TechVidvan.loopsDemo;\npublic class ForLoopDemo\n{\n  public static void main(String args[])\n  {\n    int x;\n    for( x = 25 ; ; x-- )\n      System.out.println(\u201cThis is an infinite loop\u201d);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is an infinite loop&#8230;<\/div>\n<p>Similarly, we can also skip all three expressions to create an infinite loop:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for( ; ; ; )\nloop body<\/pre>\n<p><strong>1.4. Empty Loop<\/strong><\/p>\n<p>When there is no statement in the loop-body of the loop, then it is called an empty loop. In such cases, a Java loop contains an empty statement that is, a null statement. Following for loop is an example of an empty loop:<\/p>\n<p>for( j = 20 ; j &gt;=0 ; j&#8211; ) ; \/\/<em>See,the loop body contains a null statement<\/em><\/p>\n<p>An empty for loop has its applications in the time delay loop where you need to increment or decrement the value of some variable without doing anything else, just for introducing some delay.<\/p>\n<p><strong>1.5. Declaration of variables inside loops<\/strong><\/p>\n<p>When we declare any variable inside for loop, we can not access the variable after the loop statement is over. The reason is that as the variable is declared within a block of statement its scope becomes the body of the loop. Therefore, we can\u2019t access it outside the loop body.<\/p>\n<p>Following code explains this concept:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.TechVidvan.loopsDemo;\npublic class ForLoopDemo\n{\n  public static void main(String args[])\n  {\n    for(int x = 25 ; x&gt;=0; x -= 5)\n    {\n      System.out.println(\"Inside the loop\");\n    }\n    System.out.println(x); \/\/Accessing x after the loop body gives an error\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problem:<br \/>\nx cannot be resolved to a variable<br \/>\nat project1\/com.TechVidvan.loopsDemo.ForLoopDemo.main(ForLoopDemo.java:11)<\/div>\n<p><em>In the above program, the statement System.out.println(x); is invalid as the scope of x is over. A variable is not accessible outside its scope, that\u2019s why there is an error.<\/em><\/p>\n<h4>2. The while Loop<\/h4>\n<p>The next loop available in Java is the while loop. The while loop is an entry-controlled loop.<\/p>\n<p><strong>The syntax or general form of while loop is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">while(test-expression)\nloop-body<\/pre>\n<p>In a while loop, the loop-body may contain a single, compound or an empty statement. The loop repeats while the test expression or condition evaluates to true. When the expression becomes false, the program control passes to the line just after the end of the loop-body code.<\/p>\n<p>In a while loop, a loop variable must be initialized before the loop begins. And the loop variable should be updated inside the while loop\u2019s body.<\/p>\n<p>Following code shows the working of a while loop:<\/p>\n<p><strong>Code Snippet to illustrate while loop:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/program to calculate the factorial of a number\n\npackage com.TechVidvan.loopsDemo;\npublic class WhileLoopDemo\n{\n  public static void main(String args[])\n  {\n    long i = 0, fact = 1, num = 5 ;\n    i = num ;\n    while(num != 0)\n    {\n      fact = fact * num;\n      --num;\n    }\n    System.out.println(\"The factorial of \" + i + \" is: \" +fact);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The factorial of 5 is: 120<\/div>\n<p>In the above code, as long as the value of num is non-zero, the loop body gets iterated that is, the variable. Each time the value of fact gets updated when it is multiplied with num, then the next operation is the decrement in value of num.<\/p>\n<p>And after that, again the test-expression (num) is executed. If it is false, the loop is terminated otherwise repeated.<\/p>\n<p>The following figure outlines the working of a while loop:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-while-loop-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-76523\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-while-loop-in-java.jpg\" alt=\"while loop in Java\" width=\"596\" height=\"410\" \/><\/a><\/p>\n<h5>The while loop Variations<\/h5>\n<p>A while loop also has several variations. We will discuss each of these variations:<\/p>\n<p><strong>2.1. Empty while Loop<\/strong><\/p>\n<p>An empty while loop does not contain any statement in its body. It just contains a <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/cpp\/null-statement?view=vs-2019\">null statement<\/a> which is denoted by a semicolon after the while statement:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">:\nlong wait = 0;\nwhile( ++wait &lt; 10000)\n; \/\/null statement\n:<\/pre>\n<p>The above code is a time delay loop. The time delay loop is useful for pausing the program for some time. For instance, if an important message flashes on the screen and before you can read it, it goes off.<\/p>\n<p>So, here you can introduce a time delay loop so that you get sufficient time to read the message.<\/p>\n<p><strong>2.2. Infinite while Loop<\/strong><\/p>\n<p>A while loop can be an infinite loop if you skip writing the update statement inside its body. For example, the following code is an example of an infinite while loop:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package loopsDemo;\npublic class WhileLoopDemo\n{\n  public static void main(String args[])\n  {\n    int j = 0;\n    while(j &lt;= 10)\n    {\n      System.out.println( j * j);\n    }\n    j++;\n\n    \/\/writing the update expression outside the loop body makes an infinite loop\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n0 \u2026<\/div>\n<p>The above loop is an infinite loop as the increment statement j++ is not included inside the loop\u2019s body. The value of j remains the same (that is, 0) and the loop can never terminate.<\/p>\n<p>We can also write boolean value true inside the while statement to make an infinite while loop. As condition will always be true, the loop body will get executed infinitely. It is shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">while(true)\nSystem.out.println(\u201cThis is an infinite loop\u201d);<\/pre>\n<h4>3. The do-while Loop<\/h4>\n<p>Unlike the for and while loops, the do-while loop is an exit-controlled loop which means a do-while loop evaluates its test-expression or test-condition at the bottom of the loop after executing the statements in the loop-body.<\/p>\n<p>This means the do-while loop always executes at least once !!<\/p>\n<h5><strong>Need for do-while loop:<\/strong><\/h5>\n<p>In the <strong>for<\/strong> and <strong>while<\/strong> loops, the condition is evaluated before executing the loop-body. The loop body never executes if the test expression evaluates to false for the first time itself.<\/p>\n<p>But in some situations, we want the loop-body to execute at least once, no matter what is the initial state of the test-expression. In such cases, the <strong>do-while<\/strong> loop is the best option.<\/p>\n<p><strong>The syntax or general form of do-while loop is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">do\n{\n    statement(s);\n\n} while(test-expression) ;<\/pre>\n<p>The braces <strong>{ }<\/strong> are not necessary when the loop-body contains a single statement.<\/p>\n<p>Following code shows the working of a do-while loop:<\/p>\n<p><strong>Code Snippet to illustrate the do-while loop:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/program to print all upper case letters\npackage com.TechVidvan.loopsDemo;\npublic class DoWhileLoopDemo\n{\n  public static void main(String args[])\n  {\n    char ch = 'A' ;\n\n    do\n    {\n      System.out.println( ch + \" \" );\n      ch++;\n    } while(ch &lt;= 'Z');\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">A B C D E F G H I J K L M N O P Q R S T U V W X Y Z<\/div>\n<p>The above code print characters from \u2018A\u2019 onwards until the condition ch&lt;= \u2018Z\u2019 becomes false.<\/p>\n<p>The following figure outlines the working of a do-while loop:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-do-while-loop-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-76524\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/the-do-while-loop-in-java.jpg\" alt=\"do-while loop in Java\" width=\"558\" height=\"444\" \/><\/a><\/p>\n<p>The\u200c \u200cdo-while\u200c \u200cloop\u200c \u200cis\u200c most commonly used \u200cin\u200c \u200cthe\u200c \u200cmenu\u200c \u200cselection\u200c \u200csystems,\u200c \u200cin which the user can see the menu at least once.\u200c \u200cThen\u200c \u200caccording\u200c \u200cto\u200c \u200cthe\u200c \u200cuser\u2019s\u200c \u200cresponse,\u200c \u200cit\u200c \u200cis\u200c \u200ceither\u200c \u200crepeated\u200c \u200cor\u200c \u200cterminated.\u200c \u200c<\/p>\n<h4>Nested Loops in Java<\/h4>\n<p>When a loop contains another loop in its body than it is called a nested loop. But in a nested loop, the inner loop must terminate before the outer loop. The following is an example of \u201cnested\u201d for loop:<\/p>\n<p><strong>Code to illustrate nested for loop:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.TechVidvan.loopsDemo;\npublic class NestedLoopDemo\n{\n  public static void main(String args[])\n  {\n    int i, j;\n    for( i = 1 ; i &lt;= 5 ; ++i)\n    {\t\t\t\t\t\/\/outer loop\n      System.out.println();\n      for( j = 1 ; j &lt;= i ; ++j)\t\/\/inner loop\n      System.out.println( \"* \" );\n    }\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">*<br \/>\n* *<br \/>\n* * *<br \/>\n* * * *<br \/>\n* * * * *<\/div>\n<h3>Summary<\/h3>\n<p>The Loops in Java helps a programmer to save time and effort. Repetition of statements causes a delay in time. So, loops help us to do the tasks in an easy and efficient manner. In this article, we discussed the three types of loops: for, while and do-while loop.<\/p>\n<p>We covered them with the help of examples and code snippets so that you can understand them better. Also, we have discussed the variations and special cases in the for and while loops. We also covered the concepts of nested loops in the article.<\/p>\n<p>Thank you for reading our article. I hope this article will help you to strengthen your concepts in Java loops.<\/p>\n<p>Do share your feedback through the comment section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever forgot to do your homework and as a punishment you were asked to write &#8220;I will do my homework on time.&#8221; for at least 40-50 times? It was boring as well&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":76531,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[1681,1682,1683,1684,1685,1686,1687,1688,1689,1690,1691],"class_list":["post-76370","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-do-while-loops-in-java","tag-elements-in-java-loop","tag-empty-loop-in-java","tag-for-loop-in-java","tag-infinite-loop-in-java","tag-java-loops","tag-loops-in-java","tag-needs-of-java-loops","tag-nested-loops-in-java","tag-types-of-loops-in-java","tag-while-loop-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Loops - A Complete Guide for Beginners! - TechVidvan<\/title>\n<meta name=\"description\" content=\"In this article you will learn everything about Java Loops. Along with the elements, need &amp; the 3 types of Loops in Java with their sub categories &amp; examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Loops - A Complete Guide for Beginners! - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In this article you will learn everything about Java Loops. Along with the elements, need &amp; the 3 types of Loops in Java with their sub categories &amp; examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-13T04:21:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/loops-in-java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Loops - A Complete Guide for Beginners! - TechVidvan","description":"In this article you will learn everything about Java Loops. Along with the elements, need & the 3 types of Loops in Java with their sub categories & examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-loops\/","og_locale":"en_US","og_type":"article","og_title":"Java Loops - A Complete Guide for Beginners! - TechVidvan","og_description":"In this article you will learn everything about Java Loops. Along with the elements, need & the 3 types of Loops in Java with their sub categories & examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-loops\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-02-13T04:21:54+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/loops-in-java.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java Loops &#8211; A Complete Guide for Beginners!","datePublished":"2020-02-13T04:21:54+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/"},"wordCount":2261,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/loops-in-java.jpg","keywords":["do while loops in java","Elements in Java Loop","Empty Loop in Java","for loop in java","Infinite Loop in Java","java loops","Loops in java","Needs of Java Loops","Nested Loops in Java","Types of Loops in Java","while loop in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-loops\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/","url":"https:\/\/techvidvan.com\/tutorials\/java-loops\/","name":"Java Loops - A Complete Guide for Beginners! - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/loops-in-java.jpg","datePublished":"2020-02-13T04:21:54+00:00","description":"In this article you will learn everything about Java Loops. Along with the elements, need & the 3 types of Loops in Java with their sub categories & examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-loops\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/loops-in-java.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/loops-in-java.jpg","width":802,"height":420,"caption":"types of loops in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java Loops &#8211; A Complete Guide for Beginners!"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/76370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=76370"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/76370\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/76531"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=76370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=76370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=76370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}