{"id":3598,"date":"2022-05-20T11:14:22","date_gmt":"2022-05-20T11:14:22","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3598"},"modified":"2022-05-20T11:14:22","modified_gmt":"2022-05-20T11:14:22","slug":"dart-loops","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/20\/dart-loops\/","title":{"rendered":"Dart Loops"},"content":{"rendered":"\n<p>Dart Loop is used to run a block of code repetitively for a given number of times or until matches the specified condition. Loops are essential tools for any programming language. It is used to iterate the Dart iterable such as list, map, etc. and perform operations for multiple times. A loop can have two parts &#8211; a body of the loop and control statements. The main objective of the loop is to run the code multiple times.\u00a0Dart\u00a0supports the following type of loops.<\/p>\n\n\n\n<ul><li>Dart for loop<\/li><li>Dart for\u2026in loop<\/li><li>Dart while loop<\/li><li>Dart do-while loop<\/li><\/ul>\n\n\n\n<p>We describe a brief introduction to the dart loops as follows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart for loop<\/h2>\n\n\n\n<p>The for loop is used when we know how many times a block of code will execute. It is quite same as the\u00a0C for loop. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for(Initialization; condition; incr\/decr) {  \n\/\/ loop body  \n}<\/code><\/pre>\n\n\n\n<p>The loop iteration starts from the initial value. It executes only once.<\/p>\n\n\n\n<p>The condition is a test-expression and it is checked after each iteration. The for loop will execute until false returned by the given condition.<\/p>\n\n\n\n<p>The incr\/decr is the counter to increase or decrease the value.<\/p>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  \n{  \n    int num = 1;  \n    for(num; num&lt;=10; num++)           \/\/for loop to print 1-10 numbers  \n    {  \n        print(num);     \/\/to print the number  \n    }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dart for\u2026 in Loop<\/h2>\n\n\n\n<p>The for\u2026in loop is slightly different from the for loop. It only takes dart object or expression as an iterator and iterates the element one at a time. The value of the element is bound to var, which is and valid and available for the loop body. The loop will execute until no element left in the iterator. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (var in expression) {  \n\/\/statement(s)  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Example :<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  \n{  \n    var list1 = &#91;10,20,30,40,50];  \n    for(var i in list1)           \/\/for..in loop to print list element  \n    {  \n        print(i);       \/\/to print the number  \n    }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n30\n40\n50\n<\/code><\/pre>\n\n\n\n<p>We need to declare the iterator variable to get the element from the iterator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart while loop<\/h2>\n\n\n\n<p>The while loop executes a block of code until the given expression is false. It is more beneficial when we don&#8217;t know the number of execution. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(condition) {  \n   \/\/ loop body  \n}  <\/code><\/pre>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  \n{  \n    var a = 1;  \n           var maxnum = 10;  \n           while(a&lt;maxnum){        \/\/ it will print until the expression return false  \n                         print(a);  \n                         a = a+1;                                  \/\/ increase value 1 after each iteration  \n}  \n}   <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dart do\u2026while Loop<\/h2>\n\n\n\n<p>The do\u2026while loop is similar to the while loop but only difference is that, it executes the loop statement and then check the given condition. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>do {  \n    \/\/ loop body  \n} while(condition);  <\/code><\/pre>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-loops#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  \n{  \n var a = 1;  \n var maxnum = 10;  \ndo  \n    {                \n       print(\"The value is: ${a}\");  \n       a = a+1;                                    \n       }while(a&lt;maxnum);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The value is: 1\nThe value is: 2\nThe value is: 3\nThe value is: 4\nThe value is: 5\nThe value is: 6\nThe value is: 7\nThe value is: 8\nThe value is: 9\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Selection of the loop<\/h2>\n\n\n\n<p>The selection of a loop is a little bit difficult task to the programmer. It is hard to decide which loop will be more suitable to perform a specific task. We can determine the loop based on the following points.<\/p>\n\n\n\n<ul><li>Analyze the problem and observe that whether you need a pre-test loop or post-test loop. A pre-test loop is that, the condition is tested before entering the loop. In the post-test loop, the condition is tested after entering the loop.<\/li><li>If we require a pre-test loop, then select the while or for loop.<\/li><li>If we require a post-test loop, then select the do-while loop.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Dart Loop is used to run a block of code repetitively for a given number of times or until matches the specified condition. Loops are essential tools for any programming language. It is used to iterate the Dart iterable such as list, map, etc. and perform operations for multiple times. A loop can have two [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[916],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3598"}],"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=3598"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3598\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}