{"id":3607,"date":"2019-05-25T00:01:59","date_gmt":"2019-05-25T07:01:59","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3607"},"modified":"2019-06-01T07:38:26","modified_gmt":"2019-06-01T14:38:26","slug":"falling-through-a-switch-case-structure","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3607","title":{"rendered":"Falling Through a Switch-Case Structure"},"content":{"rendered":"<p>C language keywords often used in a <em>switch<\/em>-case structure are <em>switch<\/em>, <em>case<\/em>, <em>default<\/em>, and <em>break<\/em>. Only <em>switch<\/em> and <em>case<\/em> are required. You can omit the <em>default<\/em> condition. And the <em>break<\/em> keyword is used to avoid execution falling through the structure, yet sometimes that may be a useful effect.<br \/>\n<!--more--><br \/>\nFor example, when you must match multiple conditions, stacking up a series of <em>case<\/em> statements allows you to match the multiple conditions to the same set of statements. This trick is used often for menu systems:<\/p>\n<p><code>switch(input)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case 'X':<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case 'x':<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case 'Q':<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case 'q':<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* quit\/exit the program *\/<br \/>\n...<\/code><\/p>\n<p>These four <em>case<\/em> comparisons match both upper and lower case letters X and Q. The same statements execute whether the value of variable of variable <code>input<\/code> is X, x, Q, or q.<\/p>\n<p>As another example, the following code attempts to slice up a string into individual words. A clutch of <em>case<\/em> statements match various characters that might appear after a word in a sentence. Execution falls through each one so that one set of statements executes for all the matching conditions.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"Hello there strange little planet\\n\";\r\n    int x = 0;\r\n\r\n    while( string[x] )\r\n    {\r\n        switch( string[x] )\r\n        {\r\n            case ' ':\r\n            case '.':\r\n            case ',':\r\n            case '!':\r\n            case '?':\r\n            case ';':\r\n            case ':':\r\n            case '\\n':\r\n                putchar('\\n');\r\n                break;\r\n            default:\r\n                putchar( string[x] );\r\n        }\r\n        x++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop processes the entire string. Within the loop, a <em>switch-case<\/em> structure examines various characters. If the character is a word separator, a newline is output otherwise the character in the string is output, building a word.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Hello<br \/>\nthere<br \/>\nstrange<br \/>\nlittle<br \/>\nplanet<\/code><\/p>\n<p>The fall-through ensures that more code isn&#8217;t required to process the various word-terminating characters. Further, if you forgot a character, you could easily add another <em>case<\/em> statement to match it.<\/p>\n<p>Naturally, this code isn&#8217;t without its problems.<\/p>\n<p>First, it might be easier to use a ctype function to check for word boundaries. Still, if I were coding this type of solution off the top of my head, I&#8217;d probably use a <em>switch-case<\/em> structure instead of a ctype function.<\/p>\n<p>Second, the code catches spaces between words well, but has difficulty when two separators fall between words. So if a word is followed by a comma and a space, the output shows a blank line.<\/p>\n<p>As a test, if array <code>string[]<\/code> is changed to read <code>\"Hello there strange, little planet\\n\"<\/code>, the output looks like this:<\/p>\n<p><code>Hello<br \/>\nthere<br \/>\nstrange<\/p>\n<p>little<br \/>\nplanet<\/code><\/p>\n<p>The blank line is caused by the space that follows the comma: For each character a newline is output.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3612\">next week&#8217;s Lesson<\/a>, I present a clever trick to resolve this issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can take advantage of the fall-through in a <em>switch-case<\/em> structure, which almost seems like a feature. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3607\">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-3607","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\/3607","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=3607"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3607\/revisions"}],"predecessor-version":[{"id":3636,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3607\/revisions\/3636"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}