{"id":69,"date":"2019-01-16T18:09:41","date_gmt":"2019-01-16T18:09:41","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=69"},"modified":"2019-03-02T04:36:18","modified_gmt":"2019-03-02T04:36:18","slug":"12-java-loops","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/","title":{"rendered":"Java &#8211; Loops"},"content":{"rendered":"<p>We would learn about loops in this lesson. Hence we would cover the following 5 topics.<\/p>\n<ol>\n<li><a href=\"#t1\">Introduction to Loops in Java<\/a><\/li>\n<li><a href=\"#t2\">while Loop<\/a><\/li>\n<li><a href=\"#t3\">for Loop<\/a><\/li>\n<li><a href=\"#t4\">do&#8230;while Loop<\/a><\/li>\n<li><a href=\"#t5\">Loop Control Statements<\/a><\/li>\n<li><a href=\"#t6\">Enhanced for Loop<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Introduction to Loops in Java<\/strong><\/h4>\n<p>You would probably need to execute a block of statements\u00a0 a number of times.\u00a0 This can be based on a given criteria. Loops provide a construct to achieve that.<\/p>\n<p>A loop repeatedly tests a condition. As a result, it continues to execute the block of code. This is so long as the condition is true. We call the block on code the body of the loop.<\/p>\n<p>A loop in java is represented as shown in Figure 1.0.<\/p>\n<figure id=\"attachment_70\" aria-describedby=\"caption-attachment-70\" style=\"width: 369px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-70 \" src=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java.jpg\" alt=\"Loops in Java\" width=\"369\" height=\"416\" srcset=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java.jpg 643w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java-266x300.jpg 266w\" sizes=\"auto, (max-width: 369px) 100vw, 369px\" \/><figcaption id=\"caption-attachment-70\" class=\"wp-caption-text\">Figure 1.0: Structure of Loops in Java<\/figcaption><\/figure>\n<p>We would consider three types of loops in Java:<\/p>\n<ul>\n<li>while loop<\/li>\n<li>for loop<\/li>\n<li>do&#8230;while loop<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. while Loop<\/strong><\/h4>\n<p>We use the while loop to continually execute a code as long as a condition is true. Keep in mind that the number of times to repeat the execution is probably not known.<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax of while loop is:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>condition<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t  <span style=\"color: #888888;\">\/\/body of the loop<\/span>\r\n  <span style=\"color: #333333;\">}\r\n<span style=\"color: #888888;\">\/\/other statements<\/span><\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>I have also provided a diagram for while loop in Figure 1.1 below.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_71\" aria-describedby=\"caption-attachment-71\" style=\"width: 284px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-71 \" src=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/While-Loop-in-Java.jpg\" alt=\"While Loop in Java\" width=\"284\" height=\"382\" srcset=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/While-Loop-in-Java.jpg 543w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/While-Loop-in-Java-223x300.jpg 223w\" sizes=\"auto, (max-width: 284px) 100vw, 284px\" \/><figcaption id=\"caption-attachment-71\" class=\"wp-caption-text\">Figure 1.1: While Loop in Java<\/figcaption><\/figure>\n<p>The condition is an expression that results in true or false. So when the loop starts, it first checks the condition. If it is true, then it executes the codes in the body of the loop. It then checks the condition again and if, true, it executes the body of the loop again. The process continues.<\/p>\n<p>If on the other hand, it&#8217;s false, then the loop terminates. Execution then continues with other statements after the loop.<\/p>\n<p>&nbsp;<\/p>\n<p>Listing 1.0 provides a code for using a while loop to display number 1 to 10<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> <span style=\"color: #888888;\">\/\/Loop variable<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(i<\/span> <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">);<\/span>\r\n\t  i<span style=\"color: #333333;\">++;<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: Program to display 0 to 10 using while loop<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. for Loop<\/strong><\/h4>\n<p>Next we are going to examine the for loop. You use a for loop when the you want to execute the code block a given number of times.\u00a0 So the important thing is the number of times. This is known in advance.<\/p>\n<p>The syntax for the for loop is given below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span>initialization<span style=\"color: #333333;\">;<\/span> condition<span style=\"color: #333333;\">;<\/span> update<span style=\"color: #333333;\">)<\/span>\r\n\t  <span style=\"color: #888888;\">\/\/body of the loop\t      <\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The four main parts of the for loop are:<\/p>\n<p><strong>Initialization:<\/strong> Here, you declare and initialize the loop variable.<\/p>\n<p><strong>condition:<\/strong> Here, you specify the condition that would be checked each iteration. An iteration is a single execution of the body of the loop. Therefore, this should be a boolean expression. It must evaluate to true or false. If this condition ever becomes false, then the loop terminates. As such, execution continues with the statements after the loop.<\/p>\n<p><strong>update:<\/strong> This is the code that adjusts the loop variable. It results in an increase or decrease to the loop variable<\/p>\n<p><strong>Body of the loop<\/strong>: Finally the body of the loop contains statements that are executed for each iteration of the loop<\/p>\n<p>&nbsp;<\/p>\n<p>I provide a diagram for the for-loop in Figure 1.2.<\/p>\n<figure id=\"attachment_72\" aria-describedby=\"caption-attachment-72\" style=\"width: 326px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-72 \" title=\"For loop in Java\" src=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/For-Loop-in-Java.jpg\" alt=\"For loop in Java\" width=\"326\" height=\"460\" srcset=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/For-Loop-in-Java.jpg 543w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/For-Loop-in-Java-213x300.jpg 213w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><figcaption id=\"caption-attachment-72\" class=\"wp-caption-text\">Figure 1.2: For Loop in Java<\/figcaption><\/figure>\n<p>I also provide a for loop version of the previous program. The program prints number 0 to 10 using a for loop.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">&lt;=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">);<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.2: Program to display 0 to 10 using for loop<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. do&#8230;while Loop<\/strong><\/h4>\n<p>The third and final type of loop we would cover is the do&#8230;while loop. It is similar to the while loop. Unlike the while loop, it is guaranteed to execute a least once.<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax for the do&#8230;while loop is:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #008800; font-weight: bold;\">do<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t  <span style=\"color: #888888;\">\/\/body of the loop<\/span>\r\n  <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>condition<span style=\"color: #333333;\">)\r\n<span style=\"color: #888888;\">\/\/other codes<\/span><\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>It is noteworthy that the condition (boolean expression) is placed at the end of the loop. For this reason, the loop must execute once, before it tests the condition.<\/p>\n<p>If the condition if true, then the body of the loop is executed again. I however, the condition becomes false, the loop terminates. Then other codes is executed.<\/p>\n<p>I provide the diagram for the <em>do&#8230;while<\/em> loop below.<\/p>\n<figure id=\"attachment_73\" aria-describedby=\"caption-attachment-73\" style=\"width: 296px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-73 \" src=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/DoWhile-Loop.jpg\" alt=\"do...while loop\" width=\"296\" height=\"320\" srcset=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/DoWhile-Loop.jpg 533w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/DoWhile-Loop-278x300.jpg 278w\" sizes=\"auto, (max-width: 296px) 100vw, 296px\" \/><figcaption id=\"caption-attachment-73\" class=\"wp-caption-text\">Figure 1.3: do&#8230;while Loop<\/figcaption><\/figure>\n<p>Notice that in the do&#8230;while loop, the position of the condition and the code to execute is interchanged. You can find a program to print numbers 0 to 10 using the do&#8230;while loop.&#8217;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">do<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">);<\/span>\r\n\t  i<span style=\"color: #333333;\">++;<\/span>\r\n  <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>i <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">);<\/span>dition<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>Listing 1.3: Program to display numbers 0 to 10 using do..while loop<\/p>\n<h4><strong id=\"t5\">5. Loop Control Statements<\/strong><\/h4>\n<p>Let&#8217;s now look at loop control statements. You use loop control statement to alter the normal execution sequence of a loop. So there are two of them.<\/p>\n<ul>\n<li>break Statement<\/li>\n<li>continue Statement<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>break Statement<\/strong><\/p>\n<p>You use the break statement is used to end the execution of a loop. Hence, if a break statement is encountered, the loop terminates. Then the statements after the loop is executed.<\/p>\n<p>Furthermore, the break statement can also be used with the switch statement. We would cover switch statements in the next lesson.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>continue Statement<\/strong><\/p>\n<p>You use the continue statement to terminate the current iteration of a loop. Remember that iteration means a single execution of a loop body. So let&#8217;s assume a loop body has 5 lines (line 1 to 5). If it the program is currently executing line 3 and encounters a break statement. I would skip lines 4 and 5 and jump back to line 1.<\/p>\n<ul>\n<li>In a for loop, the continue statement immediately moves control to the update\u00a0 statement<\/li>\n<li>In a while loop however, the continue statements causes the program to jump to the condition statement<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Enhanced for Loop<\/strong><\/h4>\n<p>Now this is an interesting one. We use enhanced for loop as a shorter form of the for loop. You probably would use it to loop through array elements.<\/p>\n<p>I provide the syntax below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #888888;\">\/\/Enhanced for loop<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span>declaration <span style=\"color: #333333;\">:<\/span> expression<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t  <span style=\"color: #888888;\">\/\/ body of the loop<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Declaration<\/strong>: This is a variable declaration. You make sure that the data type is compatible with the elements of the array we want to loop.<\/p>\n<p><strong>Expression<\/strong>: Specifies the array we want to loop through<\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s now use the enhanced for loop to print out all the names in an array of names. You can see this in Listing 1.4. I recommend you try it yourself.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Tester<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    String names<span style=\"color: #333333;\">[]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"background-color: #fff0f0;\">\"Osoo\"<\/span><span style=\"color: #333333;\">,<\/span><span style=\"background-color: #fff0f0;\">\"Adii\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Jackie\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Ottie\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Haty\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Kany\"<\/span><span style=\"color: #333333;\">,<\/span><span style=\"background-color: #fff0f0;\">\"Olie\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Saffy\"<\/span><span style=\"color: #333333;\">};<\/span>\r\n\t    \r\n\t    <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span>String <span style=\"color: #997700; font-weight: bold;\">name:<\/span> names<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    \tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">);<\/span>\r\n\t    <span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.4: Enhanced for loop<\/p>\n<p>&nbsp;<\/p>\n<p>I ran this program using Eclipse IDE and the output is given below in Figure 1.4:<\/p>\n<figure id=\"attachment_74\" aria-describedby=\"caption-attachment-74\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-74 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Enhanced-For-Loop-in-Java-1024x541.jpg\" alt=\"Enhanced For Loop in Java\" width=\"640\" height=\"338\" srcset=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Enhanced-For-Loop-in-Java-1024x541.jpg 1024w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Enhanced-For-Loop-in-Java-300x159.jpg 300w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Enhanced-For-Loop-in-Java-768x406.jpg 768w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Enhanced-For-Loop-in-Java.jpg 1111w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-74\" class=\"wp-caption-text\">Figure 1.4: Enhanced For Loop in Java<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>We would learn about loops in this lesson. Hence we would cover the following 5 topics. Introduction to Loops in Java while Loop for Loop &hellip; <\/p>\n","protected":false},"author":395,"featured_media":75,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - Loops - Java Tutorials<\/title>\n<meta name=\"description\" content=\"This tutorial cover loops in Java. for loop, while loop, do...while loop as well as the enhanced for loop is covered. Break and Continues statemes as well\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/java\/12-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 - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"This tutorial cover loops in Java. for loop, while loop, do...while loop as well as the enhanced for loop is covered. Break and Continues statemes as well\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-16T18:09:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-02T04:36:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"950\" \/>\n\t<meta property=\"og:image:height\" content=\"518\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; Loops\",\"datePublished\":\"2019-01-16T18:09:41+00:00\",\"dateModified\":\"2019-03-02T04:36:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/\"},\"wordCount\":1028,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Loops-in-Java-1.jpg\",\"articleSection\":[\"Java Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/\",\"name\":\"Java - Loops - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Loops-in-Java-1.jpg\",\"datePublished\":\"2019-01-16T18:09:41+00:00\",\"dateModified\":\"2019-03-02T04:36:18+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"This tutorial cover loops in Java. for loop, while loop, do...while loop as well as the enhanced for loop is covered. Break and Continues statemes as well\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Loops-in-Java-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Loops-in-Java-1.jpg\",\"width\":950,\"height\":518,\"caption\":\"Loops in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/12-java-loops\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; Loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - Loops - Java Tutorials","description":"This tutorial cover loops in Java. for loop, while loop, do...while loop as well as the enhanced for loop is covered. Break and Continues statemes as well","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:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/","og_locale":"en_US","og_type":"article","og_title":"Java - Loops - Java Tutorials","og_description":"This tutorial cover loops in Java. for loop, while loop, do...while loop as well as the enhanced for loop is covered. Break and Continues statemes as well","og_url":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/","og_site_name":"Java Tutorials","article_published_time":"2019-01-16T18:09:41+00:00","article_modified_time":"2019-03-02T04:36:18+00:00","og_image":[{"width":950,"height":518,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java-1.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; Loops","datePublished":"2019-01-16T18:09:41+00:00","dateModified":"2019-03-02T04:36:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/"},"wordCount":1028,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java-1.jpg","articleSection":["Java Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/","url":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/","name":"Java - Loops - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java-1.jpg","datePublished":"2019-01-16T18:09:41+00:00","dateModified":"2019-03-02T04:36:18+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"This tutorial cover loops in Java. for loop, while loop, do...while loop as well as the enhanced for loop is covered. Break and Continues statemes as well","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java-1.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Loops-in-Java-1.jpg","width":950,"height":518,"caption":"Loops in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/12-java-loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; Loops"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":105,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/69\/revisions\/105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/75"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}