{"id":7963,"date":"2016-08-22T10:27:09","date_gmt":"2016-08-22T09:27:09","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=7963"},"modified":"2026-03-26T16:49:36","modified_gmt":"2026-03-26T20:49:36","slug":"take-a-break-the-break-statement-in-c","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2016\/08\/22\/take-a-break-the-break-statement-in-c\/","title":{"rendered":"Take a break &#8211; the break statement in C"},"content":{"rendered":"<p>It is a given that structured programming &#8211; in one form or another &#8211; is a Good Thing. For years, the spaghetti of <strong>GOTO<\/strong>s that arose in earlier programming languages has been abhorred by all right-thinking programmers. There are good reasons for this attitude. Firstly, structured code tends to be easier to debug and, second, the code is more readable and, hence, maintainable. Both of these are productivity improvements, so the <strong>GOTO<\/strong> should clearly be avoided.<\/p>\n<p>But, what about the <strong>break<\/strong> statement in C? &#8230;<!--more--><\/p>\n<p>The <strong>break<\/strong> statement has two, closely related uses in C: it enables each \u201cclause\u201d of a <strong>switch<\/strong> statement to be exited neatly; it enables premature exit from loop\/logic constructs.<\/p>\n<p>In a <strong>switch<\/strong> statement, it is hard to imagine a sensible alternative in most cases. This code, for example:<\/p>\n<pre>switch (ss)\n{\ncase 1:\n&nbsp;&nbsp;&nbsp;alpha();\n&nbsp;&nbsp;&nbsp;break;\ncase 5:\n&nbsp;&nbsp;&nbsp;beta();\n&nbsp;&nbsp;&nbsp;break;\ncase 11:\n&nbsp;&nbsp;&nbsp;gamma();\n&nbsp;&nbsp;&nbsp;break;\ndefault:\n&nbsp;&nbsp;&nbsp;panic();\n&nbsp;&nbsp;&nbsp;break;\n};\n<br>\n<\/pre>\n<p>is quite clear. Though I guess some might argue that the last <strong>break<\/strong> is redundant. It actually makes the code more maintainable and has no effect on the resulting executable, so I feel that it should stay.<\/p>\n<p>In a complex loop &#8211; or even quite a simple one &#8211; there may be a need for premature exit. This can be done by a \u201ckluge\u201d &#8211; messing with the loop counter\/conditions, for example &#8211; but that is unwise. Or a \u201cstructured\u201d exit process may be included, thus:<\/p>\n<pre>for (i=0, exitflag=FALSE; i&lt;100 &amp;&amp; !exit_flag; i++)\n{\n&nbsp;&nbsp;&nbsp;retcode = myfunction();\n&nbsp;&nbsp;&nbsp;if (retcode == -1)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exitflag=TRUE;\n}\n<br>\n<\/pre>\n<p>However, I feel that this is much easier to follow:<\/p>\n<pre>for (i=0; i&lt;100; i++)\n{\n&nbsp;&nbsp;&nbsp;retcode = myfunction();\n&nbsp;&nbsp;&nbsp;if (retcode == -1)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;\n}\n<br>\n<\/pre>\n<p>If you have a view, please get in touch by email or comment or via social media. I have my tin helmet at the ready.<\/p>\n<p><a href=\"http:\/\/www.linkedin.com\/in\/colinwalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6579\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/linkedin.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/twitter.com\/colin_walls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6583\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/twitter.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/www.facebook.com\/colinwalls.author\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6591\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/facebook.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/plus.google.com\/116301748426290440139\/posts?hl=en%3Fhl=en\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6587\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/google.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"http:\/\/www.slideshare.net\/ColinWalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6595\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/slideshare.jpg\" alt=\"\" width=\"41\" height=\"41\" \/><\/a><a href=\"http:\/\/blogs.mentor.com\/colinwalls\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6599\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/wordpress.jpg\" alt=\"\" width=\"44\" height=\"44\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is a given that structured programming &#8211; in one form or another &#8211; is a Good Thing. For years,&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,424,339,300,340,478],"industry":[],"product":[],"coauthors":[],"class_list":["post-7963","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-debugging","tag-development-tools","tag-embedded-software","tag-programming-languages","tag-sourcery-codebench"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=7963"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7963\/revisions"}],"predecessor-version":[{"id":10501,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7963\/revisions\/10501"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=7963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=7963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=7963"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=7963"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=7963"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=7963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}