{"id":161,"date":"2013-07-13T00:01:41","date_gmt":"2013-07-13T08:01:41","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=161"},"modified":"2013-07-13T11:53:31","modified_gmt":"2013-07-13T19:53:31","slug":"for-ever","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=161","title":{"rendered":"For Ever"},"content":{"rendered":"<p>The most basic and perhaps ancient of programming techniques is the loop. And the most basic loop is the <code>for<\/code> loop. It&#8217;s also pretty flexible.<br \/>\n<!--more--><br \/>\nAll loops have three major parts:<\/p>\n<ul>\n<li>The setup<\/li>\n<li>The counter<\/li>\n<li>The terminating event<\/li>\n<\/ul>\n<p>Each of these parts can operate independently, all together, or neglected completely. In a <code>for<\/code> loop, they work like this:<\/p>\n<p><code>for(<em>setup<\/em>,<em>terminating_event<\/em>,<em>counter<\/em>)<\/code><\/p>\n<p>The other piece of the loop is the repeating statement or group of statements. The counter and terminating event can take place in those statements as well. In fact, the setup can take place before the loop even starts. So in C you&#8217;re effectively left with the following structure:<\/p>\n<p><code>for(;;)<\/code><\/p>\n<p>I read that as &#8220;for ever,&#8221; as it defines an endless loop. Observe that even though elements are missing, the semicolons are still required. And if you don&#8217;t want the loop to spin forever, you simply code the three conditions elsewhere:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x=1;                   \/* the setup *\/\r\n\r\n    for(;;)\r\n    {\r\n        printf(\"%d\\n\",x++);    \/* x++ is the counter *\/\r\n        if(x&gt;10)               \/* the exit condition *\/\r\n            break;\r\n    }\r\n    return(0);\r\n}<\/pre>\n<p>On the other side of pulling everything out of the <code>for<\/code> statement is the concept of cramming everything into it. Consider the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=1;x&lt;=10;printf(\"%d\\n\",x++))\r\n        ;\r\n    return(0);\r\n}<\/pre>\n<p>Above, all the action takes place at Line 7. The <em>printf()<\/em> statement is okay as the counter, effectively a statement within a statement. After all, isn&#8217;t <code>x++<\/code> a statement?<\/p>\n<p>The output from the two code samples is the same:<\/p>\n<p><code>1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n8<br \/>\n9<br \/>\n10<\/code><\/p>\n<p>The difference between the examples is simply the approach. I would argue that neither of the examples are as readable as doing the loop the traditional way:<\/p>\n<p><code><\/p>\n<pre>for(x=1;x<=10;x++)\r\n    printf(\"%d\\n\",x);<\/pre>\n<p><\/code><\/p>\n<p>That's pretty much what most coders expect. In fact, I don't know of any advantage to deliberately writing the code any other way, other than it's funky and non-traditional.<\/p>\n<p>Speaking of non-traditional, you can use commas to pack even more items into the <code>for<\/code> statement:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x,y;\r\n\r\n    for(x=1,y=20;x&lt;10;x++,y--)\r\n        printf(\"%d\\t%d\\n\",x,y);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In Line 7, the first and last parts of the <code>for<\/code> statement have two items, separated by a comma. The terminating event (middle item) doesn't need two conditions as the compiler would ignore the one on the left.<\/p>\n<p>What you see in this example is effectively two loops working in one statement: <code>x<\/code> counts up from 1 through 10 and <code>y<\/code> counts down from 20 to 11. Here's the output:<\/p>\n<p><code><\/p>\n<pre>1\t20\r\n2\t19\r\n3\t18\r\n4\t17\r\n5\t16\r\n6\t15\r\n7\t14\r\n8\t13\r\n9\t12\r\n10\t11<\/pre>\n<p><\/code><\/p>\n<p>I don't know of a limit on the number of items you can place in a <code>for<\/code> statement. Also, you don't have to pair up the items. For example, I've written code where the first part of the <code>for<\/code> statement had two items but last part had only one.<\/p>\n<p>Perhaps this quirk remains one of the trivial parts of the C language. It's something I'd recommend using with caution as not every programmer recognizes it, and loading up a <code>for<\/code> statement does tend to obfuscate your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s more to the <em>for<\/em> loop than meets the eye. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=161\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-161","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\/161","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=161"}],"version-history":[{"count":12,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}