{"id":5737,"date":"2023-02-08T00:01:55","date_gmt":"2023-02-08T08:01:55","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5737"},"modified":"2023-02-04T10:57:42","modified_gmt":"2023-02-04T18:57:42","slug":"a-compact-for-loop-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5737","title":{"rendered":"A Compact <em>for<\/em> Loop &#8211; Solution"},"content":{"rendered":"<p>I hope you were able to cram a lot of expressions into a <em>for<\/em> loop statement, which is the challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5726\">this month&#8217;s Exercise<\/a>. Even if you know this trick, it&#8217;s important to understand it&#8217;s limitations &mdash; which is something I discovered during my research.<br \/>\n<!--more--><br \/>\nHere is my solution, which uses an empty <em>for<\/em> loop because all the statements that would otherwise roost inside a <em>for<\/em> loop block are stuffed in the <em>for<\/em> statement itself:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_02-Exercise.c\" rel=\"noopener\" target=\"_blank\">2023_02-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int r,count;\r\n\r\n    <span class=\"comments\">\/* loop until r = 99 *\/<\/span>\r\n    for( srand((unsigned)time(NULL)), r=0, count=1 ;\r\n            r!=99 ;\r\n            count++, r=rand()%100, printf(\"%3d : %2d\\n\",count,r)\r\n       )\r\n        ;\r\n    printf(\"%3d : %2d\\n\",count,r);\r\n\r\n    return(0);\r\n}\r\n<\/pre>\n<p>At Line 10, I split the three parts of a <em>for<\/em> loop statement into three separate lines. First, the initialization:<\/p>\n<p><code>srand((unsigned)time(NULL)), r=0, count=1 ;<\/code><\/p>\n<p>You might expect to see the <em>srand()<\/em> function in a statement before the <em>for<\/em> loop. I stuck it into the looping statement on a whim and &mdash; guess what? &mdash; it works! Not every compiler allows you to stick a function into the <em>for<\/em> loop statement in this manner, but <em>clang<\/em> in the Windows Subsystem for Linux seems to be tolerant. The other two items are variable initialization expressions: <code>r=0, count=1<\/code>. I tried to declare these variables in the <em>for<\/em> loop statement, but the compiler threw a hissy fit.<\/p>\n<p><code>r!=99 ;<\/code><\/p>\n<p>The loop repeats as long as the value of <code>r<\/code> (initialized to zero) isn&#8217;t equal to 99. This expression is typical for a <em>for<\/em> loop termination, nothing fancy.<\/p>\n<p>The final part of the <em>for<\/em> loop statement contains three expressions that normally would appear in the <em>for<\/em> loop block:<\/p>\n<p><code>count++, r=rand()%100, printf(\"%3d : %2d\\n\",count,r)<\/code><\/p>\n<p>Variable <code>count<\/code> is incremented. A random value <code>r<\/code> is fetched in the range zero through 99. A <em>printf()<\/em> statement outputs the program&#8217;s progress. Some compilers may balk at setting an assignment expression (<code>r=rand()%100<\/code> into a <em>for<\/em> loop statement. Even so, the point of this exercise is to condense the loop as much as you can. If the compiler doesn&#8217;t allow it, but you tried it anyway, give yourself a <em>C For Dummies<\/em> bonus point.<\/p>\n<p>The final <em>printf()<\/em> statement is required to output the value 99 after it&#8217;s encountered. Otherwise, the loop ends before this value is output.<\/p>\n<p>I hope your solution met with success and you perhaps learned a bit about how whacky you can get with a <em>for<\/em> statement. I wouldn&#8217;t cram so much stuff into the statement for the programs I write. Yet, doing so is allowed (by some compilers) and a fun way to prove how ridiculously cryptic the C programming language can be.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you were able to cram a lot of expressions into a for loop statement, which is the challenge for this month&#8217;s Exercise. Even if you know this trick, it&#8217;s important to understand it&#8217;s limitations &mdash; which is something &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5737\">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":[5],"tags":[],"class_list":["post-5737","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5737","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=5737"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5737\/revisions"}],"predecessor-version":[{"id":5748,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5737\/revisions\/5748"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}