{"id":2926,"date":"2018-02-03T00:01:09","date_gmt":"2018-02-03T08:01:09","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2926"},"modified":"2018-01-27T11:03:45","modified_gmt":"2018-01-27T19:03:45","slug":"is-continue-necessary","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2926","title":{"rendered":"Is <em>continue<\/em> Necessary?"},"content":{"rendered":"<p>On this blog, I try to show examples for all the C language keywords, but <em>continue<\/em> is one I struggle with. It doesn&#8217;t appear in <em>Beginning Programming with C For Dummies<\/em>. It barely appears in my <em>C All-in-one Desktop Reference For Dummies<\/em>. Even the venerable K&#038;R manual has difficulty explaining how this keyword is worthy.<br \/>\n<!--more--><br \/>\nIt&#8217;s said that <em>continue<\/em> is like the <em>break<\/em> keyword in that it interrupts the flow of a <em>for<\/em>, <em>while<\/em>, or <em>do<\/em> loop. Unlike <em>break<\/em>, <em>continue<\/em> directs the loop to keep spinning &mdash; providing that the loop&#8217;s condition remains true.<\/p>\n<p>The problem I see is that you can&#8217;t do anything with <em>continue<\/em> that you could otherwise do with an <em>if-else<\/em> structure. In fact, I don&#8217;t believe I&#8217;ve ever seen <em>continue<\/em> used without an <em>if<\/em> statement. That evidence kinda drives home the point for me.<\/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=1;x&lt;11;x++)\r\n    {\r\n        printf(\"%d\",x);\r\n        if(x%5==0)\r\n        {\r\n            putchar('\\n');\r\n            continue;\r\n        }\r\n        printf(\", \");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code above outputs two rows of numbers. The <code>if(x%5==0)<\/code> statement breaks the output with a newline every 5 numbers. The <em>continue<\/em> statement directs the loop to repeat, skipping over the final <em>printf()<\/em> statement. Here&#8217;s the output:<\/p>\n<pre><code>1, 2, 3, 4, 5\r\n6, 7, 8, 9, 10<\/code><\/pre>\n<p>You could, however, re-write the <em>if<\/em> block as follows:<\/p>\n<pre class=\"screen\">\r\nif(x%5==0)\r\n    putchar('\\n');\r\nelse\r\n    printf(\", \");<\/pre>\n<p>One way I can figure that <em>continue<\/em> would be useful is in a loop with lots of statements and nested conditions. In that setup, <em>continue<\/em> saves you from coding a complex <em>if-else<\/em> structure just to bail out.<\/p>\n<p>Yes, and I suppose the same argument I&#8217;ve made about <em>continue<\/em> can also be made about the <em>break<\/em> keyword. It&#8217;s possible to code without it, but having it available makes it easier to work in loops, break out of them &mdash; or continue looping &mdash; without a lot of complex overhead.<\/p>\n<p>If you can think of a way to use <em>continue<\/em> that&#8217;s unique or clever, let me know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Of the assorted C language keywords, one you&#8217;ll seldom if ever use is <em>continue<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2926\">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-2926","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\/2926","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=2926"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2926\/revisions"}],"predecessor-version":[{"id":2952,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2926\/revisions\/2952"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}