{"id":6263,"date":"2024-03-02T00:01:02","date_gmt":"2024-03-02T08:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6263"},"modified":"2024-02-24T12:40:25","modified_gmt":"2024-02-24T20:40:25","slug":"fun-with-switch-case-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6263","title":{"rendered":"Fun with <em>switch case<\/em>, Part II"},"content":{"rendered":"<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6253\">last week&#8217;s Lesson<\/a>, I reviewed the <em>switch-case<\/em> structure and how declared constants aren&#8217;t truly the kind of constant a <em>case<\/em> statement requires. This week I continue my exploration of <em>switch-case<\/em>, with an interesting and surprising quirk of the <em>default<\/em> condition.<br \/>\n<!--more--><br \/>\nTo review, the <em>default<\/em> condition is what&#8217;s executed when none of the <em>case<\/em> statements yield a match. It&#8217;s labeled like <em>case<\/em>, and works the same:<\/p>\n<p><code>default:<\/code><\/p>\n<p>Most programmers know that the <em>default<\/em> statement is optional. After all, it&#8217;s called a <em>switch-case<\/em> structure and not <em>switch-case-default<\/em>. If you don&#8217;t need a <em>default<\/em> condition, it can be omitted. But another interesting and obscure fact is that the <em>default<\/em> statement can dwell anywhere in the <em>switch<\/em> block.<\/p>\n<p>Traditionally, coders put <em>default<\/em> last. I believe it&#8217;s because the progression is how we think, but also it kind of mirrors an <em>if-else<\/em> statement, with <em>default<\/em> handling the &#8220;<em>else<\/em>&#8221; part. Even so, you don&#8217;t have to put <em>default<\/em> last, as the following code demonstrates.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_03_02-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_03_02-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n\r\n    printf(\"1, 2, 3: \");\r\n    scanf(\"%d\",&amp;a);\r\n\r\n    switch(a)\r\n    {\r\n        case 1:\r\n            puts(\"One!\");\r\n            break;\r\n        default:\r\n            puts(\"You bad!\");\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    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Normally, I would expect to see <code>case 1<\/code>, <code>case 2<\/code>, <code>case 3<\/code>, and then <em>default<\/em>. But above, I snuck the <em>default<\/em> part amid the <em>case<\/em> statements. I have no justification for doing so, only to prove that the code builds and runs.<\/p>\n<p>In fact, it might make more sense to put <em>default<\/em> first, though my point is that it can go anywhere. A rule doesn&#8217;t exist in C programming that <em>default<\/em> must come last &mdash; if it&#8217;s used at all.<\/p>\n<p>I&#8217;m unaware whether this trick works in other programming languages. Seeing how most languages have their roots in C, my guess is that it does.<\/p>\n<p>Another interesting perspective on <em>switch-case<\/em> is that it offers a primitive form of recursion. This observation makes more sense than comparing <em>switch-case<\/em> to a stacked <em>if-else if-else<\/em> construction, though such a comparison is a better way to teach the topic.<\/p>\n<p>Still, the processor repeatedly &#8220;calls&#8221; each <em>case<\/em> statement, evaluating them until a solution is found. This observation makes sense when you think of how the mechanics work. It also makes it interesting to think of how <em>default<\/em> is handled if internally the <em>switch-case<\/em> structure is evaluated recursively.<\/p>\n<p>Knowing this tidbit, moving forward I&#8217;ll continue to set <em>default<\/em> as the final part of a <em>switch-case<\/em> structure. I&#8217;m familiar with its location last. Though when I&#8217;m feeling spicy, I may put it atop the structure &mdash; just to see whether anyone notices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>default<\/em> condition in a <em>switch-case<\/em> structure is optional, but that&#8217;s not its only quality. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6263\">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-6263","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\/6263","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=6263"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6263\/revisions"}],"predecessor-version":[{"id":6282,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6263\/revisions\/6282"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}