{"id":986,"date":"2014-11-01T00:01:24","date_gmt":"2014-11-01T07:01:24","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=986"},"modified":"2014-10-25T08:53:08","modified_gmt":"2014-10-25T15:53:08","slug":"how-a-for-loop-works","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=986","title":{"rendered":"How a <em>for<\/em> Loop Works"},"content":{"rendered":"<p>I received an interesting question the other day. A student asked me to explain a <em>for<\/em> loop. Dutifully, I set out and gave my description, which he found entirely baffling. So I backed up again and gave this explanation:<br \/>\n<!--more--><\/p>\n<blockquote><p>A <em>for<\/em> loop repeats one or more statements a given number of times.<\/p><\/blockquote>\n<p>Originally, I spoke about initializing the loop, setting iteration conditions, and of course how to exit. That description was way above his head. He just wanted to know the secret to repeating a chunk of code. That got me thinking.<\/p>\n<p>When I first learned to program, I didn&#8217;t know how a <em>for<\/em> loop worked. All I knew was the where to plug the repeat value:<\/p>\n<p><code>for(x=0;x&lt;<span style=\"color:red\">50<\/span>;x++)<\/code><\/p>\n<p>To make a loop repeat <span style=\"color:red\">50<\/span> times, use the above code. If you need the loop to repeat 4 times, change the <span style=\"color:red\">50<\/span> to a <span style=\"color:red\">4<\/span>. Oh, and you need to declare <code>x<\/code> as an <em>int<\/em> variable earlier in the code. Aside from that, the loop is done: Just ignore all the other characters, symbols, and junk. <em>Ta-da<\/em>!<\/p>\n<p>Often my desire to teach comes out strong in my explanation. Yet, not everyone who studies C programming wants to learn. They just want to pass the course. That was the case for this student, who really didn&#8217;t want to understand the details of a <em>for<\/em> loop or explore programming concepts. So be it.<\/p>\n<p>More simple loop statements exist. For example, the LOGO language has a <em>repeat<\/em> keyword. Using C language syntax, it works like this:<\/p>\n<pre><code>repeat(50)\r\n{\r\n    puts(\"Hello, there!\");\r\n}<\/code><\/pre>\n<p>Sometimes that&#8217;s all you need, no fancy anything. Of course, the <em>for<\/em> keyword offers you a lot of flexibility, and that&#8217;s where the C language gets its low-level power. You can do more with a <em>for<\/em> loop if you need to do more, which is why the C language lacks a <em>repeat<\/em> keyword.<\/p>\n<p>On a simple level, however, when you need to repeat a chunk of code, or a single statement, you simply use this format:<\/p>\n<p><code>for(x=0;x&lt;<span style=\"color:red\"><em>n<\/em><\/span>;x++)<\/code><\/p>\n<p>Replace the <span style=\"color:red\"><em>n<\/em><\/span> with a repeat value, and you&#8217;re on your way. You haven&#8217;t learned anything about programming, but you&#8217;re on your way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Forget the rubbish, here is how a <em>for<\/em> loop works. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=986\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-986","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=986"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/986\/revisions"}],"predecessor-version":[{"id":1057,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/986\/revisions\/1057"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}