{"id":2178,"date":"2016-10-22T00:01:53","date_gmt":"2016-10-22T07:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2178"},"modified":"2016-10-15T12:10:56","modified_gmt":"2016-10-15T19:10:56","slug":"looping-variables-end-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2178","title":{"rendered":"Looping Variables End Value"},"content":{"rendered":"<p>I received a question on a looping variable and why its value <em>after<\/em> the loop isn&#8217;t the same as the ending value of the loop. It&#8217;s a puzzling issue I remember from when I first learned to program, but one that you can understand once you figure out what the loop does.<br \/>\n<!--more--><br \/>\nAs an example, consider a basic <em>for<\/em> loop, such as the one shown in 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 i;\r\n\r\n    for(i=0;i&lt;10;i++)\r\n        printf(\"The value of 'i' is %d\\n\",i);\r\n\r\n    printf(\"And after the loop, 'i' is %d\\n\",i);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop repeats 10 times. Variable <code>i<\/code> measures the count, starting at <code>i=0<\/code> and looping 10 times &mdash; as long as the value of <code>i<\/code> is less than 10, or <code>i&lt;10<\/code>.<\/p>\n<p>So the loop stops when?<\/p>\n<p>It stops when the value of <code>i<\/code> is <strong>10 or greater<\/strong>. Therefore, after the loop is complete, the value of <code>i<\/code> isn&#8217;t equal to 9, rather it&#8217;s equal to 10 or greater. Here&#8217;s the output:<\/p>\n<pre><code>The value of 'i' is 0\r\nThe value of 'i' is 1\r\nThe value of 'i' is 2\r\nThe value of 'i' is 3\r\nThe value of 'i' is 4\r\nThe value of 'i' is 5\r\nThe value of 'i' is 6\r\nThe value of 'i' is 7\r\nThe value of 'i' is 8\r\nThe value of 'i' is 9\r\nAnd after the loop, 'i' is 10<\/code><\/pre>\n<p>The only time this effect is confusing is when you forget that the counter does increment one last time when the loop doesn&#8217;t repeat. So at the top of the loop (the <em>for<\/em> statement), variable <code>i<\/code> is equal to 10. Then the loop skips and the next statement is processed.<\/p>\n<p>The same effect holds true when you plow through an array or a string. The last time the loop tries to execute, the counter variable is one greater than what was specified. So if the loop ends on the <code>\\0<\/code> (null character) at the end of a string, the pointer is still referencing that character after the loop is done. That is:<\/p>\n<p><code>while(ch != '\\0')<\/code><\/p>\n<p>The above loop stops when <code>ch<\/code> contains (or points at) the character <code>\\0<\/code>.<\/p>\n<p>The key to understanding this effect, is to slowly digesting the individual pieces of a loop, specifically the counter variables. Never assume that the variable is anything after the loop is complete until you look at the looping statement. When you&#8217;re in doubt, add a test statement in the code (such as the one in the example) to demonstrate to yourself what the ending counter variable value really is. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The value isn&#8217;t what you expect, but if you study the problem it all makes perfect sense. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2178\">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-2178","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\/2178","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=2178"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2178\/revisions"}],"predecessor-version":[{"id":2185,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2178\/revisions\/2185"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}