{"id":1225,"date":"2023-05-21T07:07:24","date_gmt":"2023-05-21T07:07:24","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.com\/?p=1225"},"modified":"2023-05-21T07:07:24","modified_gmt":"2023-05-21T07:07:24","slug":"how-to-use-loops-in-javascript","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2023\/05\/21\/how-to-use-loops-in-javascript\/","title":{"rendered":"How to use Loops in Javascript?"},"content":{"rendered":"\n<p>There are mainly four types of loops in JavaScript.<\/p>\n\n\n\n<p>for loop<\/p>\n\n\n\n<p>for\/in a loop (explained later)<\/p>\n\n\n\n<p>while loop<\/p>\n\n\n\n<p>do\u2026while loop<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">for loop<\/h4>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for(statement1; statement2; statment3)\n \n{\n \nlines of code to be executed\n \n}\n<\/pre>\n\n\n\n<ol>\n<li>Statement1 is executed first, even before executing the looping code. So, this statement is normally used to assign values to variables used inside the loop.<\/li>\n\n\n\n<li>The statement2 is the condition to execute the loop.<\/li>\n\n\n\n<li>The statement3 is executed every time after the looping code is executed.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html&gt;\n&lt;head&gt;\n    &lt;script type=\"text\/javascript\"&gt;\n        var students = new Array(\"John\", \"Ann\", \"Aaron\", \"Edwin\", \"Elizabeth\");\n        document.write(\"&lt;b&gt;Using for loops &lt;\/b&gt;&lt;br \/&gt;\");\n        for (i=0;i&lt;students.length;i++)\n        {\n        document.write(students[i] + \"&lt;br \/&gt;\");\n        }\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">while loop<\/h4>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">while(condition)\n\n{\n\nlines of code to be executed\n\n}<\/pre>\n\n\n\n<p>The \u201cwhile loop\u201d is executed as long as the specified condition is true. Inside the while loop, you should include the statement that will end the loop at some point in time. Otherwise, your loop will never end, and your browser may crash.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">do\u2026while loop<\/h4>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;pre&gt;\ndo\n\n{\n\nblock of code to be executed\n\n} while (condition)\n<\/pre>\n\n\n\n<p>The do\u2026while loop is very similar to the while loop. The only difference is that in do\u2026while loop, the block of code gets executed once even before checking the condition.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html&gt;\n&lt;head&gt;\n    &lt;script type=\"text\/javascript\"&gt;\n        document.write(\"&lt;b&gt;Using while loops &lt;\/b&gt;&lt;br \/&gt;\");\n        var i = 0, j = 1, k;\n        document.write(\"Fibonacci series less than 40&lt;br \/&gt;\");\n        while(i&lt;40)\n        {\n            document.write(i + \"&lt;br \/&gt;\");\n            k = i+j;\n            i = j;\n            j = k;\n        }\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are mainly four types of loops in JavaScript. for loop for\/in a loop (explained&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1225"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=1225"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1225\/revisions"}],"predecessor-version":[{"id":1226,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1225\/revisions\/1226"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=1225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=1225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=1225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}