{"id":3122,"date":"2018-06-02T00:01:54","date_gmt":"2018-06-02T07:01:54","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3122"},"modified":"2022-01-01T11:03:10","modified_gmt":"2022-01-01T19:03:10","slug":"multiple-statements-in-a-for-condition","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3122","title":{"rendered":"Multiple Expressions in a <em>for<\/em> Condition"},"content":{"rendered":"<p>Every C programmer has screwed up a <em>for<\/em> loop&#8217;s statement. Having three arguments in one set of parentheses is just begging for trouble. That&#8217;s a lot of yard waste, but there&#8217;s a reason for all the detritus.<br \/>\n<!--more--><br \/>\nBefore I dive into it, here&#8217;s how the <em>for<\/em> statement is described in my book, <em>Beginning Programming with C For Dummies<\/em>:<\/p>\n<p><code>for(<em>initialization<\/em>; <em>exit_condition<\/em>; <em>repeat_each<\/em>)<\/code><\/p>\n<p>Each item is separated by a semicolon. Occasionally I forget how it works ( \ud83d\ude41 ) and I use a comma or colon. It happens.<\/p>\n<p>Anything other than two semicolons in the statement raises the compiler&#8217;s eyebrows and the code, hopefully, doesn&#8217;t compile. But the code can compile if you add commas and take advantage of a neat <em>for<\/em> loop trick.<\/p>\n<p>This code uses <em>for<\/em> to repeat a chunk of code 10 times, displaying values for variables <code>x<\/code> and <code>y<\/code>:<\/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    y = 0;\r\n    for(x=0;x&lt;10;x++)\r\n    {\r\n        printf(\"%2d:%2d\\n\",x,y);\r\n        y+=2;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre><code> 0: 0\r\n 1: 2\r\n 2: 4\r\n 3: 6\r\n 4: 8\r\n 5:10\r\n 6:12\r\n 7:14\r\n 8:16\r\n 9:18<\/code><\/pre>\n<p>Cinchy.<\/p>\n<p>The versatility of the <em>for<\/em> loop lets you do more than just play with the looping variable <code>x<\/code> inside its parentheses. You can also add an <em>initialization<\/em> and <em>repeat_each<\/em> statement to handle variable <code>y<\/code>. Consider this modification:<\/p>\n<p><code>for(x=0,y=0;x&lt;10;x++,y+=2)<\/code><\/p>\n<p>Yes, the statement is quite cryptic and complex. The initialization part contains two expressions separated by a comma: <code>x=0<\/code> and <code>y=0<\/code>. This double expression is legal in the C language.<\/p>\n<p>The loop&#8217;s <em>exit_condition<\/em> remains unchanged.<\/p>\n<p>The <em>for<\/em> loop&#8217;s <em>repeat_each<\/em> portion also has two expressions: <code>x++<\/code> and <code>y+=2<\/code>, separated by using a comma.<\/p>\n<p>With these modifications, the loop is reduced to a single 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=0,y=0;x&lt;10;x++,y+=2)\r\n        printf(\"%2d:%2d\\n\",x,y);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program&#8217;s output is the same.<\/p>\n<p>Many C programmers delight in this type of cryptic statement. It saves lines of code and makes things look mysterious and cryptic, which they enjoy. I&#8217;ve used the comma a few times in my own code to shorten things up. It&#8217;s a nifty trick, but do be aware how it affects your code&#8217;s readability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a reason why the <em>for<\/em> loop&#8217;s statement uses semicolons instead of commas. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3122\">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-3122","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\/3122","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=3122"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3122\/revisions"}],"predecessor-version":[{"id":5153,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3122\/revisions\/5153"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}