{"id":1538,"date":"2015-09-12T00:01:31","date_gmt":"2015-09-12T07:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1538"},"modified":"2015-09-05T10:25:49","modified_gmt":"2015-09-05T17:25:49","slug":"to-increment-before-or-after","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1538","title":{"rendered":"To Increment Before or After"},"content":{"rendered":"<p>Say you have a <em>for<\/em> loop that increments one notch each time it repeats. I code such a loop in this fashion:<\/p>\n<pre><code>for(x=0;x<10;x++)<\/code><\/pre>\n<p>More common, however, programmers use this approach:<\/p>\n<pre><code>for(x=0;x<10;++x)<\/code><\/pre>\n<p>The difference is in how the variable <code>x<\/code> is incremented. I put the <code>++<\/code> after the <code>x<\/code>, but most coders put it before. What the deal?<br \/>\n<!--more--><br \/>\nWhether you specify <code>x++<\/code> or <code>++x<\/code> is merely a matter of preference. That's because the value of <code>x<\/code> isn't being assigned to something. In the <em>for<\/em> loop's guts, the <code>x++<\/code> or <code>++x<\/code> is its own statement.<\/p>\n<p>In their seminal work, <em>The C Programming Language<\/em>, Brian Kernighan and Dennis Ritchie use <code>++x<\/code> in their <em>for<\/em> loop examples. I read that book when I was first learning C, as well as multiple other books. I can only assume that the last book I read &mdash; the one where C finally clicked for me &mdash; showed <code>x++<\/code> in a <em>for<\/em> loop. That's my best guess.<\/p>\n<p>Whether you use one approach or the other, the value of variable <code>x<\/code> is incremented the same as the <em>for<\/em> loop spins. Here's sample code for my method:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=0;x&lt;10;x++)\r\n        printf(\"%d\\n\",x);\r\n    printf(\"%d\\n\",x);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I ran this code to check the value of variable <code>x<\/code> inside the loop and when the loop is done. Here's the output:<\/p>\n<pre><code>0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10<\/code><\/pre>\n<p>If I modify Line 7 to read <code>for(x=0;x&lt;10;x++)<\/code>, the output is the same: The loop starts with <code>x<\/code> equal to 0 and ends with <code>x<\/code> equal to 9. After the loop is done, in both cases, <code>x<\/code> equals 10.<\/p>\n<p>Consider this issue one of preference only. It matters not whether <code>x<\/code> is incremented before or after, the results are the same.<\/p>\n<p>In a way, this choice is similar to how I write my <em>return<\/em> statements. I use parentheses but a lot of C coders choose not to. The parentheses optional when <em>return<\/em> specifies a single value, so using one or the other is simply a matter of preference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The difference between using <code>x++<\/code> or <code>++x<\/code> in a <em>for<\/em> loop. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1538\">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-1538","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\/1538","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=1538"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1538\/revisions"}],"predecessor-version":[{"id":1559,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1538\/revisions\/1559"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}