{"id":1717,"date":"2016-01-16T00:01:20","date_gmt":"2016-01-16T08:01:20","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1717"},"modified":"2016-01-17T12:05:58","modified_gmt":"2016-01-17T20:05:58","slug":"give-me-a-break","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1717","title":{"rendered":"Give Me a <em>break<\/em>"},"content":{"rendered":"<p>I received an email question recently about the power of the <em>break<\/em> keyword. The answer to the question is that you can only break out of the current loop or <em>switch-case<\/em> structure. Even in a nested loop, or a <em>switch-case<\/em> structure within a loop, <em>break<\/em> affects only the current element.<br \/>\n<!--more--><br \/>\nSpecifically, the reader wanted to know if a <em>break<\/em> inside <em>switch-case<\/em> structure would also halt the <em>while<\/em> loop if the structure were inside the loop. The answer is no.<\/p>\n<p>This type of <em>while<\/em>\/<em>switch-case<\/em> construction is common in many programs. I use it frequently in my code. Sometimes I use:<\/p>\n<pre><code>while(1)<\/code><\/pre>\n<p>But more frequently I use:<\/p>\n<pre><code>while(!done)<\/code><\/pre>\n<p>Where <code>done<\/code> is a boolean value, either TRUE (1) or FALSE (0).<\/p>\n<p>The <em>switch-case<\/em> structure inside the <em>while<\/em> loop examines input. Each <em>case<\/em> part of the structure deals with a keypress or some other condition. One of the <em>case<\/em> items handles the loop&#8217;s exit condition. For example:<\/p>\n<pre class=\"screen\">\r\ncase 'q':\r\ncase 'Q':\r\n     done = TRUE;\r\n     break;<\/pre>\n<p>Above, on the <code>q<\/code> or <code>Q<\/code> input, the <code>done<\/code> variable is set to TRUE. The <em>break<\/em> statement exits the <em>switch-case<\/em> structure, but not the <em>while<\/em> loop. That loop terminates because the value of <code>done<\/code> is now TRUE.<\/p>\n<p>For an endless <em>while<\/em> loop, some other condition must be set, because you need a <em>break<\/em> statement outside the <em>switch-case<\/em> structure to terminate the loop.<\/p>\n<p>The following code example shows nested <em>while<\/em> loops. The <em>break<\/em> statements affects only the current loop:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x = 10;\r\n\r\n    puts(\"main() function\");\r\n    while(x)\r\n    {\r\n        puts(\"First while loop\");\r\n        while(x)\r\n        {\r\n            puts(\"\\tSecond while loop\");\r\n            x--;\r\n            if(x<8)\r\n                break;\r\n        }\r\n        if(x<5)\r\n            break;\r\n    }\r\n    puts(\"Back in the main() function\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The output shows how the <em>break<\/em> statement at Line 16 in the inner loop doesn't cancel the outer <em>while<\/em> loop. Only the <em>break<\/em> statement at Line 19 does that. Here is the output:<\/p>\n<pre><code>\r\nmain() function\r\nFirst while loop\r\n\tSecond while loop\r\n\tSecond while loop\r\n\tSecond while loop\r\nFirst while loop\r\n\tSecond while loop\r\nFirst while loop\r\n\tSecond while loop\r\nFirst while loop\r\n\tSecond while loop\r\nBack in the main() function<\/code><\/pre>\n<p>One more thing about the <em>break<\/em> keyword: It can dwell only within a loop or <em>switch-case<\/em> structure. The compiler generates an error if you stick a rogue <em>break<\/em> in a function. In other words, you can't use <em>break<\/em> to exit a function or terminate a program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A <em>break<\/em> statement affects only the current loop or <em>switch-case<\/em> structure. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1717\">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-1717","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\/1717","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=1717"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1717\/revisions"}],"predecessor-version":[{"id":1746,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1717\/revisions\/1746"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}