{"id":2187,"date":"2016-11-05T00:01:51","date_gmt":"2016-11-05T07:01:51","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2187"},"modified":"2016-10-29T10:32:15","modified_gmt":"2016-10-29T17:32:15","slug":"yes-you-can-nest-while-loops","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2187","title":{"rendered":"Yes, You Can Nest <em>while<\/em> Loops"},"content":{"rendered":"<p>Somewhere in my vast array of teaching material, I claimed that only <em>for<\/em> loops can be nested. That&#8217;s poppycock.<br \/>\n<!--more--><br \/>\nYou can nest any type of loop in the C language, or in any programming language. A <em>while<\/em> loop nests just as snugly as a <em>for<\/em> loop. In fact, you can convert any <em>for<\/em> loop into a <em>while<\/em> loop, no problem. Some programmers find <em>while<\/em> loops more readable, which is a sentiment I share.<\/p>\n<p>In the following code, nested <em>while<\/em> loops generate a grid of numbers and letters:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    short a;\r\n    char b;\r\n\r\n    a = 1;\r\n    while(a &lt;= 5)\r\n    {\r\n        b = 'A';\r\n        while(b &lt;= 'E')\r\n        {\r\n            printf(\"%d%c\\t\",a,b);\r\n            b++;\r\n        }\r\n        putchar('\\n');\r\n        a++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop at Line 9 spins 5 times. The <em>while<\/em> loop at Line 12 also spins 5 times, but uses <em>char<\/em> variable <code>b<\/code> and characters <code>'A'<\/code> through <code>'F'<\/code> instead of values. (Well, internally these characters are values.)<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>1A\t1B\t1C\t1D\t1E\t\r\n2A\t2B\t2C\t2D\t2E\t\r\n3A\t3B\t3C\t3D\t3E\t\r\n4A\t4B\t4C\t4D\t4E\t\r\n5A\t5B\t5C\t5D\t5E<\/code><\/pre>\n<p>A <em>for<\/em> loop can accomplish the same thing, but in fewer lines. That&#8217;s because the various pieces of the loop are contained in a single statement as opposed to separate lines in the code.<\/p>\n<p>For example, a <em>for<\/em> loop for variable <code>a<\/code> would combine Lines 8, 9, and 18 into a single statement:<\/p>\n<p><code>for(a=1; a&lt;=5; a++)<\/code><\/p>\n<p>Ditto for variable <code>b<\/code>:<\/p>\n<p><code>for(b='A'; b&lt;='E'; b++)<\/code><\/p>\n<p>Here is the for version of the same code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    short a;\r\n    char b;\r\n\r\n    for(a=1; a&lt;=5; a++)\r\n    {\r\n        for(b='A'; b&lt;='E'; b++)\r\n            printf(\"%d%c\\t\",a,b);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>And the output is the same, though the code is 6 lines shorter.<\/p>\n<p>Which is best? Neither, though again I believe that the <em>while<\/em> loop example is more readable. Still, my point is that you can nest both <em>for<\/em> and <em>while<\/em> loops. You can even mix them. If I wrote or said that you can&#8217;t nest a <em>while<\/em> loop I was just being silly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can nest any loop. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2187\">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-2187","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\/2187","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=2187"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2187\/revisions"}],"predecessor-version":[{"id":2194,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2187\/revisions\/2194"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}