
{"id":424,"date":"2018-10-02T16:42:52","date_gmt":"2018-10-02T16:42:52","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?page_id=424"},"modified":"2018-10-02T16:42:52","modified_gmt":"2018-10-02T16:42:52","slug":"switch-case-statement-glossary-entry","status":"publish","type":"page","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/switch-case-statement-glossary-entry\/","title":{"rendered":"Switch-Case statement (Glossary Entry)"},"content":{"rendered":"<p>A switch-case is used for flow control when you are testing the same variable for equality.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true\">switch (expression) {\r\n\r\n&lt;declarations&gt;\r\n\r\ncase constant:\r\n\r\nstatements\r\n\r\nbreak;\r\n\r\ncase constant:\r\n\r\nstatements\r\n\r\nbreak;\r\n\r\ndefault:\r\n\r\nstatements\r\n\r\nbreak;\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>The term \u2018expression\u2019 above refers to a variable under test. From the diagram below, it is the selector that is used to determine the flow of code within the switch-case statement.<\/p>\n<figure id=\"attachment_425\" aria-describedby=\"caption-attachment-425\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-425\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/SwitchCaseSelector.png\" alt=\"\" width=\"600\" height=\"389\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/SwitchCaseSelector.png 1481w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/SwitchCaseSelector-300x194.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/SwitchCaseSelector-768x498.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/SwitchCaseSelector-1024x664.png 1024w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-425\" class=\"wp-caption-text\">Depicting a Switch-Case as a multiplexer. The selector is used to choose the code path.<\/figcaption><\/figure>\n<p>Consider the following example:<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">int iResponse;\r\n\r\nputs(\"Please select:\");\r\nputs(\"0 - Sales\");\r\nputs(\"1 - Opening hours\");\r\nputs(\"2 - Repairs\");\r\nputs(\"3 - Complaints\");\r\nputs(\"4 - Anything else\");\r\n\r\nscanf(\"%d\", &amp;iResponse);\r\n\r\nif (iResponse == 0) {\r\n   puts(\"Please wait for a sales advisor\");\r\n   playAnnoyingMusic(5);\r\n   redirect(123);\r\n} else if (iResponse == 1) {\r\n   puts(\"We are open 24Hrs 7 days a week\");\r\n   redirect(0);\r\n} else if (iResponse == 2) {\r\n   puts(\"Please wait while we redirect you\");\r\n   playAnnoyingMusic(25);\r\n   redirect(124);\r\n} else if ((iResponse == 3) || (iResponse == 4)) {\r\n   puts(\"Please wait while we redirect you\");\r\n   playAnnoyingMusic(60);\r\n   redirect(0);\t\/\/very cynical\r\n} else {\r\n   puts(\"Returning to main menu\");\r\n   playAnnoyingMusic(5);\r\n   redirect(0);\t\/\/very cynical\r\n};\r\n<\/pre>\n<p><b>Note<\/b> how it is the same variable being tested each time. The equivalent code with a switch-case is as follows:<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">int iResponse;\r\n\r\nputs(\"Please select:\");\r\nputs(\"0 - Sales\");\r\nputs(\"1 - Opening hours\");\r\nputs(\"2 - Repairs\");\r\nputs(\"3 - Complaints\");\r\nputs(\"4 - Anything else\");\r\n\r\nscanf(\"%d\", &amp;iResponse);\r\n\r\nswitch (iResponse) {\r\n      \r\n   case 0:\r\n      puts(\"Please wait for a sales advisor\");\r\n      playAnnoyingMusic(5);\r\n      redirect(123);\r\n      break;\r\n   case 1:\r\n      puts(\"We are open 24Hrs 7 days a week\");\r\n      redirect(0);\r\n      break;\r\n   case 2:\r\n      puts(\"Please wait while we redirect you\");\r\n      playAnnoyingMusic(25);\r\n      redirect(124);\r\n      break;\r\n   case 3:\r\n   case 4:\r\n      puts(\"Please wait while we redirect you\");\r\n      playAnnoyingMusic(60);\r\n      redirect(0);\t\/\/very cynical\r\n      break;\r\n   default:\r\n      puts(\"Returning to main menu\");\r\n      playAnnoyingMusic(5);\r\n      redirect(0);\t\/\/very cynical\r\n      break;\r\n};<\/pre>\n<p><b>Notes:<\/b><\/p>\n<ul>\n<li>Each evaluation must be exclusive<\/li>\n<li>Use <span class=\"lang:default decode:true  crayon-inline \">break<\/span>\u00a0 to avoid falling through into the next case (common error)<\/li>\n<li>Multiple conditions can be handled by writing separate case statements without a break.<\/li>\n<li><span class=\"lang:default decode:true  crayon-inline \">default<\/span>\u00a0 (optional) is the catch-all if none are matched<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A switch-case is used for flow control when you are testing the same variable for equality. switch (expression) { &lt;declarations&gt; case constant: statements break; case constant: statements break; default: statements break; } &nbsp; The term \u2018expression\u2019 above refers to a variable under test. From the diagram below, it is the selector that is used to&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/switch-case-statement-glossary-entry\/\">Continue reading <span class=\"screen-reader-text\">Switch-Case statement (Glossary Entry)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":153,"menu_order":65,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-424","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":1,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/424\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/424\/revisions\/426"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/153"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}