{"id":1100,"date":"2022-02-07T19:01:20","date_gmt":"2022-02-07T19:01:20","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1100"},"modified":"2022-02-07T19:01:20","modified_gmt":"2022-02-07T19:01:20","slug":"loop-control","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/07\/loop-control\/","title":{"rendered":"Loop Control"},"content":{"rendered":"\n<p>There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.<\/p>\n\n\n\n<p>Programming languages provide various control structures that allow for more complicated execution paths.<\/p>\n\n\n\n<p>A&nbsp;<strong>loop<\/strong>&nbsp;statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages \u2212<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/java\/images\/loop_architecture.jpg\" alt=\"Loop Architecture\"\/><\/figure>\n\n\n\n<p>Java programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Sr.No.<\/th><th>Loop &amp; Description<\/th><\/tr><tr><td>1<\/td><td>while loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.<\/td><\/tr><tr><td>2<\/td><td>for loopExecute a sequence of statements multiple times and abbreviates the code that manages the loop variable.<\/td><\/tr><tr><td>3<\/td><td><a href=\"https:\/\/www.tutorialspoint.com\/java\/java_do_while_loop.htm\">do&#8230;while loop<\/a>Like a while statement, except that it tests the condition at the end of the loop body.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"loop-control-statements\">Loop Control Statements<\/h2>\n\n\n\n<p>Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.<\/p>\n\n\n\n<p>Java supports the following control statements. Click the following links to check their detail.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Sr.No.<\/th><th>Control Statement &amp; Description<\/th><\/tr><tr><td>1<\/td><td>break statementTerminates the\u00a0<strong>loop<\/strong>\u00a0or\u00a0<strong>switch<\/strong>\u00a0statement and transfers execution to the statement immediately following the loop or switch.<\/td><\/tr><tr><td>2<\/td><td>continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"enhanced-for-loop-in-java\">Enhanced for loop in Java<\/h2>\n\n\n\n<p>As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax\">Syntax<\/h3>\n\n\n\n<p>Following is the syntax of enhanced for loop \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for(declaration : expression) {\n   \/\/ Statements\n}\n<\/code><\/pre>\n\n\n\n<ul><li><strong>Declaration<\/strong>&nbsp;\u2212 The newly declared block variable, is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element.<\/li><li><strong>Expression<\/strong>&nbsp;\u2212 This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<p>Live Demo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Test {\n\n   public static void main(String args&#91;]) {\n      int &#91;] numbers = {10, 20, 30, 40, 50};\n\n      for(int x : numbers ) {\n         System.out.print( x );\n         System.out.print(\",\");\n      }\n      System.out.print(\"\\n\");\n      String &#91;] names = {\"James\", \"Larry\", \"Tom\", \"Lacy\"};\n\n      for( String name : names ) {\n         System.out.print( name );\n         System.out.print(\",\");\n      }\n   }\n}<\/code><\/pre>\n\n\n\n<p>This will produce the following result \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">10, 20, 30, 40, 50,\nJames, Larry, Tom, Lacy,<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A&nbsp;loop&nbsp;statement allows [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[238],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1100"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1100"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1100\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}