{"id":3603,"date":"2019-05-18T00:01:27","date_gmt":"2019-05-18T07:01:27","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3603"},"modified":"2019-05-25T08:19:13","modified_gmt":"2019-05-25T15:19:13","slug":"the-switch-case-fall-through","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3603","title":{"rendered":"Switch-Case Execution"},"content":{"rendered":"<p>The <em>switch-case<\/em> structure is the most complex decision tool in the C language. Yes, the ternary operator is more cryptic, but a proper <em>switch-case<\/em> structure involves more keywords: <em>switch<\/em>, <em>case<\/em>, <em>default<\/em>, and <em>break<\/em>. The <em>break<\/em> keyword is what I feel makes the structure most interesting.<br \/>\n<!--more--><br \/>\nAs a decision tool, <em>switch-case<\/em> handles multiple comparisons: The <em>switch<\/em> argument is compared with each <em>case<\/em> constant. Only when the comparison is true are the statements belonging to <em>case<\/em> executed. When no <em>case<\/em> argument matches, the statements belonging to <em>default<\/em> are executed &mdash; but even then, <em>default<\/em> is optional.<\/p>\n<p>Below is code for a typical <em>switch-case<\/em> structure. The value of variable <code>alpha<\/code> is compared to the constants set after each of the <em>case<\/em> statements.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int alpha = 5;\r\n\r\n    switch(alpha)\r\n    {\r\n        case 1:\r\n            puts(\"One\");\r\n            break;\r\n        case 2:\r\n            puts(\"Two\");\r\n            break;\r\n        case 3:\r\n            puts(\"Three\");\r\n            break;\r\n        case 4:\r\n            puts(\"Four\");\r\n            break;\r\n        case 5:\r\n            puts(\"Five\");\r\n            break;\r\n        default:\r\n            puts(\"Out of range\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The last <em>case<\/em> statement&#8217;s constant value 5 matches the value of <code>alpha<\/code>, so &#8220;Five&#8221; is output. The <em>break<\/em> statement exits the <em>switch-case<\/em> structure. Here&#8217;s the output:<\/p>\n<p><code>Five<\/code><\/p>\n<p>Here&#8217;s the output when all the <em>break<\/em> statements are removed:<\/p>\n<p><code>Five<br \/>\nOut of range<\/code><\/p>\n<p>Because none of the preceding <em>case<\/em> statements matched 5, their statements don&#8217;t execute. But because <em>break<\/em> in the <code>case 5:<\/code> statement is removed, you also see statements from the <em>default<\/em> condition executed.<\/p>\n<p>When the value of <code>alpha<\/code> is changed to 1 and the <em>break<\/em> statements are removed, here&#8217;s the output:<\/p>\n<p><code>One<br \/>\nTwo<br \/>\nThree<br \/>\nFour<br \/>\nFive<br \/>\nOut of range<\/code><\/p>\n<p>Though the value of <code>alpha<\/code> doesn&#8217;t match subsequent <em>case<\/em> statement values, execution falls through to those statements anyway.<\/p>\n<p>In a way, it helps to think of the <em>case<\/em> keyword as a label, not really a controlling part of the structure. Its value is compared, but the <em>case<\/em> statement itself isn&#8217;t a barrier; once a <em>case<\/em> statement rings true, execution cascades through the rest of the <em>switch-case<\/em> structure line-by-line. That is, unless a <em>break<\/em> statement is found.<\/p>\n<p>Further, you can&#8217;t trick the compiler by setting two <em>case<\/em> statements with the same value. (And I wouldn&#8217;t know why such a thing would be practical.) If you attempt to set two <em>case<\/em> statements with the same value, the compiler generates a duplicate case value error. Seriously, if you need more statements for a match, put them all after a single <em>case<\/em> condition.<\/p>\n<p>The fall-through effect of the <em>switch-case<\/em> structure can be an advantage. I&#8217;ll demonstrate how in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3607\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>switch case<\/em> structure is complex and powerful, but not without its quirks. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3603\">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-3603","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\/3603","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=3603"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3603\/revisions"}],"predecessor-version":[{"id":3622,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3603\/revisions\/3622"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}