{"id":302,"date":"2011-03-24T21:16:03","date_gmt":"2011-03-24T21:16:03","guid":{"rendered":"https:\/\/greladesign.co\/blog\/?p=302"},"modified":"2011-03-24T21:18:17","modified_gmt":"2011-03-24T21:18:17","slug":"useful-use-of-switch-block","status":"publish","type":"post","link":"https:\/\/greladesign.co\/blog\/2011\/03\/24\/useful-use-of-switch-block\/","title":{"rendered":"useful use of switch block"},"content":{"rendered":"<p>Hi,<\/p>\n<p>recently I have browsed through <a href=\"http:\/\/wonderfl.net\/\">wonderfl.net<\/a> and found unknown to me but useful use-case of <em>switch<\/em> block. The switch is selection control block and usually is used as follows:<br \/>\n<!--more--><\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nswitch(index)\r\n{\r\n    case 0:\r\n        \/\/block of code (to the nearest break) executed when tested statement, here index variable has value of 0.\r\n    break;\r\n    case 1:\r\n        \/\/block of code (to the nearest break) executed when tested statement, here index variable has value of 1.\r\n    break;\r\n    case 2:\r\n    case 3:\r\n        \/\/block of code (to the nearest break) executed when tested statement, here index variable has value of 2 or 3.\r\n    break;\r\n    default:\r\n        \/\/block of code executed when tested statement wasn't matched by any of &lt;em&gt;case &lt;\/em&gt;blocks.\r\n    \r\n}\r\n<\/pre>\n<p>But at the aforementioned website I have found code that used switch in different way:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nswitch (true) \r\n{\r\n\tcase index == 0:\r\n\t\t\/\/code executed when index == 0\r\n\tbreak;\r\n\tcase index == 1:\r\n\t\t\/\/code executed when index == 1\r\n\tbreak;\r\n\tcase index == 2 || index == 3:\r\n\t\t\/\/code executed when index == 2 or 3\r\n\tbreak;\r\n\tdefault:\r\n\t\t\/\/when index wasn't matched by any of case blocks\r\n}\r\n<\/pre>\n<p>this way gives more flexibility over &#8220;test cases&#8221;<\/p>\n<p>best regards<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, recently I have browsed through wonderfl.net and found unknown to me but useful use-case of switch block. The switch is selection control block and usually is used as follows:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":""},"categories":[17,9],"tags":[302,12,34,87],"_links":{"self":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/302"}],"collection":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":4,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}