{"id":4525,"date":"2015-08-26T06:18:45","date_gmt":"2015-08-26T06:18:45","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=4525"},"modified":"2015-08-26T06:18:45","modified_gmt":"2015-08-26T06:18:45","slug":"javascript-for-loop","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/javascript-for-loop\/","title":{"rendered":"Javascript-For Loop"},"content":{"rendered":"<p>The &#8216;<b>for<\/b>&#8216; loop is the most compact form of looping. It includes the following three important parts \u2212<\/p>\n<ul class=\"list\">\n<li>The <b>loop initialization<\/b> where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.<\/li>\n<li>The <b>test statement<\/b> which will test if a given condition is true or not. If the condition is true, then the code given inside the loop will be executed, otherwise the control will come out of the loop.<\/li>\n<li>The <b>iteration statement<\/b> where you can increase or decrease your counter.<\/li>\n<\/ul>\n<p>You can put all the three parts in a single line separated by semicolons.<\/p>\n<h3>Flow Chart<\/h3>\n<p>The flow chart of a for loop in JavaScript would be as follows \u2212<\/p>\n<p><img src=\"http:\/\/www.tutorialspoint.com\/javascript\/images\/for_loop.jpg\" alt=\"For Loop\" \/><\/p>\n<h3>Syntax<\/h3>\n<p>The syntax of for loop is JavaScript is as follows \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"kwd\">for<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">initialization<\/span><span class=\"pun\">;<\/span><span class=\"pln\"> test condition<\/span><span class=\"pun\">;<\/span><span class=\"pln\"> iteration statement<\/span><span class=\"pun\">){<\/span>\r\n   <span class=\"typ\">Statement<\/span><span class=\"pun\">(<\/span><span class=\"pln\">s<\/span><span class=\"pun\">)<\/span><span class=\"pln\"> to be executed <\/span><span class=\"kwd\">if<\/span><span class=\"pln\"> test condition <\/span><span class=\"kwd\">is<\/span> <span class=\"kwd\">true<\/span>\r\n<span class=\"pun\">}<\/span><\/pre>\n<h3>Example<\/h3>\n<p>Try the following example to learn how a for loop works in JavaScript.<\/p>\n<pre class=\"prettyprint notranslate tryit prettyprinted\"><span class=\"tag\">&lt;html&gt;<\/span>\r\n   <span class=\"tag\">&lt;body&gt;<\/span>\r\n      \r\n      <span class=\"tag\">&lt;script<\/span> <span class=\"atn\">type<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"text\/javascript\"<\/span><span class=\"tag\">&gt;<\/span>\r\n         <span class=\"pun\">&lt;!--<\/span>\r\n            <span class=\"kwd\">var<\/span><span class=\"pln\"> count<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\r\n            document<\/span><span class=\"pun\">.<\/span><span class=\"pln\">write<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Starting Loop\"<\/span> <span class=\"pun\">+<\/span> <span class=\"str\">\"&lt;br \/&gt;\"<\/span><span class=\"pun\">);<\/span>\r\n         \r\n            <span class=\"kwd\">for<\/span><span class=\"pun\">(<\/span><span class=\"pln\">count <\/span><span class=\"pun\">=<\/span> <span class=\"lit\">0<\/span><span class=\"pun\">;<\/span><span class=\"pln\"> count <\/span><span class=\"pun\">&lt;<\/span> <span class=\"lit\">10<\/span><span class=\"pun\">;<\/span><span class=\"pln\"> count<\/span><span class=\"pun\">++){<\/span><span class=\"pln\">\r\n               document<\/span><span class=\"pun\">.<\/span><span class=\"pln\">write<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Current Count : \"<\/span> <span class=\"pun\">+<\/span><span class=\"pln\"> count <\/span><span class=\"pun\">);<\/span><span class=\"pln\">\r\n               document<\/span><span class=\"pun\">.<\/span><span class=\"pln\">write<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"&lt;br \/&gt;\"<\/span><span class=\"pun\">);<\/span>\r\n            <span class=\"pun\">}<\/span><span class=\"pln\">\r\n         \r\n            document<\/span><span class=\"pun\">.<\/span><span class=\"pln\">write<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Loop stopped!\"<\/span><span class=\"pun\">);<\/span>\r\n         <span class=\"com\">\/\/--&gt;<\/span>\r\n      <span class=\"tag\">&lt;\/script&gt;<\/span>\r\n      \r\n      <span class=\"tag\">&lt;p&gt;<\/span><span class=\"pln\">Set the variable to different value and then try...<\/span><span class=\"tag\">&lt;\/p&gt;<\/span>\r\n   <span class=\"tag\">&lt;\/body&gt;<\/span>\r\n<span class=\"tag\">&lt;\/html&gt;<\/span><\/pre>\n<h3>Output<\/h3>\n<pre class=\"result notranslate\">Starting Loop\r\nCurrent Count : 0\r\nCurrent Count : 1\r\nCurrent Count : 2\r\nCurrent Count : 3\r\nCurrent Count : 4\r\nCurrent Count : 5\r\nCurrent Count : 6\r\nCurrent Count : 7\r\nCurrent Count : 8\r\nCurrent Count : 9\r\nLoop stopped!<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The &#8216;for&#8216; loop is the most compact form of looping. It includes the following three important parts \u2212 The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if a given condition is true or not. If the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4526,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[10],"tags":[51],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/4525"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=4525"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/4525\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media\/4526"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=4525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=4525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=4525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}