{"id":182,"date":"2009-04-28T19:57:54","date_gmt":"2009-04-28T19:57:54","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/04\/28\/switching-things-up\/"},"modified":"2009-04-28T19:58:11","modified_gmt":"2009-04-28T19:58:11","slug":"switching-things-up","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/04\/28\/switching-things-up\/","title":{"rendered":"Switching Things Up"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>If you have ever used a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/switch.html\"><tt>switch<\/tt><\/a> statement in MATLAB and also used it <a href=\"http:\/\/en.wikipedia.org\/wiki\/Switch_statement\">in C<\/a>, you might have noticed that the two constructs have different semantics.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">switch in C<\/a><\/li>\r\n         <li><a href=\"#2\">switch in MATLAB<\/a><\/li>\r\n         <li><a href=\"#3\">More on switch in MATLAB<\/a><\/li>\r\n         <li><a href=\"#4\">Do You Use switch Statements?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>switch in C<a name=\"1\"><\/a><\/h3>\r\n   <p>In C, you often terminate <tt>case<\/tt> statements in <tt>switch<\/tt> constructs with a <tt>break<\/tt> statement to prevent execution of the code in the <tt>case<\/tt> that follows.  Omitting the <tt>break<\/tt> statement causes the code in the following <tt>case<\/tt> to execute.  The behavior of <tt>switch<\/tt> statements in C is sometimes described as <i>fall through<\/i>.\r\n   <\/p>\r\n   <h3>switch in MATLAB<a name=\"2\"><\/a><\/h3>\r\n   <p>There are several major differences between <tt>switch<\/tt> statements in MATLAB and C.\r\n   <\/p>\r\n   <li>MATLAB does not have <i>fall through<\/i> behavior in its <tt>switch<\/tt> statements and therefore no <tt>break<\/tt> statement is typically required for individual <tt>case<\/tt> statementss.\r\n   <\/li>\r\n   <li>MATLAB <tt>case<\/tt> statements have another feature that C and many other languages do not have.  The expression following <tt>case<\/tt> need not be scalar-valued, but can combine several expressions by placing the acceptable expressions in a cell array.\r\n   <\/li>\r\n   <li>If you want to specify behavior for <tt>switch<\/tt> expressions that don't match any of the <tt>case<\/tt> expressions, use the <tt>otherwise<\/tt> branch. In C, use <tt>default<\/tt> instead.\r\n   <\/li>\r\n   <li>Expressions for <tt>case<\/tt> statements can include strings and numeric expressions, not just integer values.  Though you have to be careful using non-integral\r\n      numbers because of round-off, this flexibility can make the intent of your code more obvious instead of seeing a list of numbers.\r\n   <\/li>\r\n   <h3>More on switch in MATLAB<a name=\"3\"><\/a><\/h3>\r\n   <p>When we originally were designing <tt>switch<\/tt> for MATLAB, we examined much of our own C source code, and we found a huge majority of the <tt>case<\/tt> statements terminated in <tt>break<\/tt>, hence the no fall-through behavior of MATLAB. This makes the <tt>switch<\/tt> statement in MATLAB equivalent to an <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/if.html\"><tt>if-elseif<\/tt><\/a> construct.  To achieve fall-through behavior in MATLAB, you can specify all the relevant expressions in one <tt>case<\/tt> and then conditionally compute values within that section of code.\r\n   <\/p>\r\n   <h3>Do You Use switch Statements?<a name=\"4\"><\/a><\/h3>\r\n   <p>I am curious to hear about your experiences using <tt>switch<\/tt> statements in MATLAB.  Post your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=182#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_b820e3e442844563af7e2fb5b2441a6d() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b820e3e442844563af7e2fb5b2441a6d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b820e3e442844563af7e2fb5b2441a6d';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_b820e3e442844563af7e2fb5b2441a6d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.8<br><\/p>\r\n<\/div>\r\n<!--\r\nb820e3e442844563af7e2fb5b2441a6d ##### SOURCE BEGIN #####\r\n%% Switching Things Up\r\n% If you have ever used a \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/switch.html |switch|>\r\n% statement in MATLAB and also used it \r\n% <http:\/\/en.wikipedia.org\/wiki\/Switch_statement in C>, you might have \r\n% noticed that the two constructs have different semantics.\r\n%% switch in C\r\n% In C, you often terminate |case| statements in |switch| constructs with a\r\n% |break| statement to prevent execution of the code in the |case| that\r\n% follows.  Omitting the |break| statement causes the code in the following\r\n% |case| to execute.  The behavior of |switch| statements in C is sometimes\r\n% described as _fall through_.\r\n\r\n%% switch in MATLAB\r\n% There are several major differences between |switch| statements in MATLAB\r\n% and C.\r\n%\r\n% # MATLAB does not have _fall through_ behavior in its |switch| statements\r\n% and therefore no |break| statement is typically required for individual \r\n% |case| statementss.  \r\n% # MATLAB |case| statements have another feature that C\r\n% and many other languages do not have.  The expression following |case|\r\n% need not be scalar-valued, but can combine several expressions by placing\r\n% the acceptable expressions in a cell array.\r\n% # If you want to specify behavior for |switch| expressions that don't\r\n% match any of the |case| expressions, use the |otherwise| branch. In C,\r\n% use |default| instead.\r\n% # Expressions for |case| statements can include strings and numeric\r\n% expressions, not just integer values.  Though you have to be careful\r\n% using non-integral numbers because of round-off, this flexibility can\r\n% make the intent of your code more obvious instead of seeing a list of\r\n% numbers.\r\n\r\n%% More on switch in MATLAB\r\n% When we originally were designing |switch| for MATLAB, we examined much\r\n% of our own C source code, and we found a huge majority of the |case|\r\n% statements terminated in |break|, hence the no fall-through behavior of\r\n% MATLAB. This makes the |switch| statement in MATLAB equivalent to an \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/if.html |if-elseif|>\r\n% construct.  To achieve fall-through behavior in MATLAB, you can specify\r\n% all the relevant expressions in one |case| and then conditionally compute\r\n% values within that section of code.  \r\n\r\n%% Do You Use switch Statements?\r\n% I am curious to hear about your experiences using |switch| statements in\r\n% MATLAB.  Post your thoughts \r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=182#respond here>.\r\n##### SOURCE END ##### b820e3e442844563af7e2fb5b2441a6d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      If you have ever used a switch statement in MATLAB and also used it in C, you might have noticed that the two constructs have different semantics.\r\n      \r\n   \r\n   Contents\r\n ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/04\/28\/switching-things-up\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,15,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}