{"id":1533,"date":"2015-09-05T00:01:42","date_gmt":"2015-09-05T07:01:42","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1533"},"modified":"2015-08-30T10:09:01","modified_gmt":"2015-08-30T17:09:01","slug":"skipping","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1533","title":{"rendered":"Skipping"},"content":{"rendered":"<p>Of the two C language keyword looping statements, <em>for<\/em> is the most traditional and probably the most popular. It&#8217;s also the most frustrating for beginners because of its many parts. But eventually, a comfort level arises with using the <em>for<\/em> loop, which is sad because it&#8217;s more powerful than a simple counter.<br \/>\n<!--more--><br \/>\nA while back, I wrote a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=986\">post<\/a> about how to setup a basic <em>for<\/em> loop, one that repeats a given number of times. That&#8217;s really all you need to know about a loop, and in many simple programming languages, a basic loop-like statement simply repeats a given chunk of code.<\/p>\n<p>The <em>for<\/em> loop is more powerful than a simple repeat-this-chunk-of-code type of loop. In fact, the C language <em>for<\/em> keyword is on loan to just about every other programming language because it&#8217;s so flexible.<\/p>\n<p>Part of the flexibility is that you can set specific starting and ending values for the loop, and also determine a skip value. So if you need to work through only even numbers, you craft a for statement like this:<\/p>\n<pre><code>for(x=2;x<=100;x+=2)<\/code><\/pre>\n<p>The loop starts at value 2, then increments by 2 all the way up to 100. The <code>x+=2<\/code> calculation is short for <code>x=x+2<\/code>, which keeps adding 2 to the value of variable <code>x<\/code>, all the way from 2 to 100.<\/p>\n<p>Another powerful aspect of the <em>for<\/em> loop is that its conditions can vary. To demonstrate how that can work, the following code contains a nested <em>for<\/em> loop. The inner loop counts from 1 to 100 in increments from 1 to 10. The outer loop sets those increments.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int skip,x;\r\n\r\n    for(skip=1;skip<=10;skip++)\r\n    {\r\n        printf(\"Skip %d: \",skip);\r\n        for(x=skip;x&lt;=100;x+=skip)\r\n        {\r\n            printf(\" %3d\",x);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here's the output:<\/p>\n<pre><code>Skip 1:    1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100\r\nSkip 2:    2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100\r\nSkip 3:    3   6   9  12  15  18  21  24  27  30  33  36  39  42  45  48  51  54  57  60  63  66  69  72  75  78  81  84  87  90  93  96  99\r\nSkip 4:    4   8  12  16  20  24  28  32  36  40  44  48  52  56  60  64  68  72  76  80  84  88  92  96 100\r\nSkip 5:    5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100\r\nSkip 6:    6  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96\r\nSkip 7:    7  14  21  28  35  42  49  56  63  70  77  84  91  98\r\nSkip 8:    8  16  24  32  40  48  56  64  72  80  88  96\r\nSkip 9:    9  18  27  36  45  54  63  72  81  90  99\r\nSkip 10:   10  20  30  40  50  60  70  80  90 100<\/code><\/pre>\n<p>The outer loop value <code>skip<\/code> is used twice in the inner loop at Line 10. First, it sets the starting number for the loop. Second, it sets the increment. (If the starting value for the loop were set to 1, then the output wouldn't look as pretty.)<\/p>\n<p>The sample code's output isn't really anything special. It does, however, demonstrate my point, which is that a <em>for<\/em> loop need not default to the traditional and expected <code>x++<\/code> as the loop counter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not all loops need to increment by one. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1533\">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-1533","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\/1533","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=1533"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1533\/revisions"}],"predecessor-version":[{"id":1545,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1533\/revisions\/1545"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}