{"id":212,"date":"2009-12-29T13:04:20","date_gmt":"2009-12-29T13:04:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/12\/29\/a-case-for-divergence-between-matlab-and-c\/"},"modified":"2009-12-22T13:05:18","modified_gmt":"2009-12-22T13:05:18","slug":"a-case-for-divergence-between-matlab-and-c","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/12\/29\/a-case-for-divergence-between-matlab-and-c\/","title":{"rendered":"A Case for Divergence between MATLAB and C"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There are some functions in MATLAB that we designed to be similar to their counterparts in other languages.  There are also\r\n         some functions that we deliberately designed differently.  A case in point is the syntax and behavior for the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/switch.html\"><tt>switch<\/tt><\/a> statement.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">No Fall Through in MATLAB<\/a><\/li>\r\n         <li><a href=\"#2\">Why Does C Allow Fall Through?<\/a><\/li>\r\n         <li><a href=\"#3\">How Does MATLAB Handle Code Sharing Between Cases?<\/a><\/li>\r\n         <li><a href=\"#4\">What About Default?<\/a><\/li>\r\n         <li><a href=\"#5\">Example Code<\/a><\/li>\r\n         <li><a href=\"#9\">What Cases Do You Run Into?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>No Fall Through in MATLAB<a name=\"1\"><\/a><\/h3>\r\n   <p>The individual <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/case.html\"><tt>case<\/tt><\/a> statements in MATLAB do not fall through to the next <tt>case<\/tt>, the default behavior in C.  We found that in C, one common source of bugs was not properly stopping the default fall through\r\n      behavior but termination with a <tt>break<\/tt> statement.  So we eliminated the need to terminate each <tt>case<\/tt> in MATLAB.\r\n   <\/p>\r\n   <h3>Why Does C Allow Fall Through?<a name=\"2\"><\/a><\/h3>\r\n   <p>At least one major reason C allows fall through behavior is so that multiple cases can share code.  This is generally very\r\n      good for code hygiene.  Otherwise, you risk code bloat and propogation of errors via copy-paste, among other pitfalls.\r\n   <\/p>\r\n   <h3>How Does MATLAB Handle Code Sharing Between Cases?<a name=\"3\"><\/a><\/h3>\r\n   <p>MATLAB avoids the code bloat and error propagation downsides of no fall through behavior by allowing you to group cases together,\r\n      in a cell array.  In fact, if you want to share code, you must group like cases together since you cannot fall through to\r\n      another case under any circumstances.  If your expression matches more than one case, the first one encountered in the <tt>switch<\/tt> is the one that will execute.\r\n   <\/p>\r\n   <p>If you have some cases that share most of the code in common but still differ a bit, you have several options.  You can place\r\n      them in the same case and then embed another <tt>switch<\/tt> statement or use conditional logic, via <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/if.html\"><tt>if<\/tt><\/a> statements. Or write code perhaps using appropriate logical indexing.\r\n   <\/p>\r\n   <h3>What About Default?<a name=\"4\"><\/a><\/h3>\r\n   <p>Finally, we felt that the last choice in a <tt>switch<\/tt> statement, being called <tt>default<\/tt>, was a bit misleading.  It <i>is<\/i> used if no other <tt>case<\/tt> matches, but that is rarely the behavior the programmer is expecting. Instead, we chose <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/otherwise.html\"><tt>otherwise<\/tt><\/a> because we felt it better reflected that none of the previous cases matched at all.\r\n   <\/p>\r\n   <h3>Example Code<a name=\"5\"><\/a><\/h3>\r\n   <p>Here's a simple example that shows the features of <tt>switch<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">blogswitch<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction blogswitch(myinput)\r\n% Sample switch statement for blog\r\n\r\nswitch myinput\r\n    case {1, true, 'yes','true'}\r\n        disp('Input is true')\r\n    case {0, 'false', 'no', false}\r\n        disp('Input is false')\r\n    case {ones, zeros}\r\n        disp('Ya can''t get here from there.  You need to make up your mind!')\r\n    otherwise\r\n        disp('No dice')\r\nend\r\n<\/pre><p>One thing you may notice is that you can't reach the third case.  The case listed with <tt>{ones, zeros}<\/tt> will always be satisfied by one of the first two cases and won't ever execute.\r\n   <\/p>\r\n   <p>Let's try the code.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">blogswitch <span style=\"color: #A020F0\">true<\/span><\/pre><pre style=\"font-style:oblique\">Input is true\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">blogswitch <span style=\"color: #A020F0\">maybe<\/span><\/pre><pre style=\"font-style:oblique\">No dice\r\n<\/pre><h3>What Cases Do You Run Into?<a name=\"9\"><\/a><\/h3>\r\n   <p>I wonder what situations you find yourselves in.  Do you use <tt>switch<\/tt> statements with cases for multiple conditions?  Did you even know you could do that?  Let me know your <tt>switch-case<\/tt> experiences <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=212#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_c3b87fff327f4eb09e4cad0b200ec65d() {\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='c3b87fff327f4eb09e4cad0b200ec65d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' c3b87fff327f4eb09e4cad0b200ec65d';\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_c3b87fff327f4eb09e4cad0b200ec65d()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\nc3b87fff327f4eb09e4cad0b200ec65d ##### SOURCE BEGIN #####\r\n%% A Case for Divergence between MATLAB and C\r\n% There are some functions in MATLAB that we designed to be similar to\r\n% their counterparts in other languages.  There are also some functions\r\n% that we deliberately designed differently.  A case in point is the syntax\r\n% and behavior for the \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/switch.html |switch|>\r\n% statement.\r\n%% No Fall Through in MATLAB\r\n% The individual\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/case.html |case|>\r\n% statements in MATLAB do not fall through to the next |case|, the default\r\n% behavior in C.  We found that in C, one common source of bugs was not\r\n% properly stopping the default fall through behavior but termination with a \r\n% |break| statement.  So we eliminated the need to terminate each |case| in\r\n% MATLAB.\r\n%% Why Does C Allow Fall Through?\r\n% At least one major reason C allows fall through behavior is so that\r\n% multiple cases can share code.  This is generally very good for code\r\n% hygiene.  Otherwise, you risk code bloat and propogation of errors via\r\n% copy-paste, among other pitfalls.\r\n%% How Does MATLAB Handle Code Sharing Between Cases?\r\n% MATLAB avoids the code bloat and error propagation downsides of no fall\r\n% through behavior by allowing you to group cases together, in a cell\r\n% array.  In fact, if you want to share code, you must group like cases\r\n% together since you cannot fall through to another case under any\r\n% circumstances.  If your expression matches more than one case, the first\r\n% one encountered in the |switch| is the one that will execute.\r\n%\r\n% If you have some cases that share most of the code in common but still\r\n% differ a bit, you have several options.  You can place them in the same\r\n% case and then embed another |switch| statement or use conditional logic,\r\n% via\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/if.html |if|>\r\n% statements. Or write code perhaps using appropriate logical\r\n% indexing.  \r\n%% What About Default?\r\n% Finally, we felt that the last choice in a |switch| statement, being\r\n% called |default|, was a bit misleading.  It _is_ used if no other |case|\r\n% matches, but that is rarely the behavior the programmer is expecting.\r\n% Instead, we chose\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/otherwise.html |otherwise|>\r\n% because we felt it better reflected that none of the previous cases\r\n% matched at all.\r\n%% Example Code\r\n% Here's a simple example that shows the features of |switch|.\r\ntype blogswitch\r\n%% \r\n% One thing you may notice is that you can't reach the third case.  The\r\n% case listed with |{ones, zeros}| will always be satisfied by one of the\r\n% first two cases and won't ever execute.\r\n%%\r\n% Let's try the code.\r\nblogswitch true\r\n%%\r\nblogswitch maybe\r\n%% What Cases Do You Run Into?\r\n% I wonder what situations you find yourselves in.  Do you use |switch|\r\n% statements with cases for multiple conditions?  Did you even know you\r\n% could do that?  Let me know your |switch-case| experiences\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=212#respond here>.\r\n##### SOURCE END ##### c3b87fff327f4eb09e4cad0b200ec65d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There are some functions in MATLAB that we designed to be similar to their counterparts in other languages.  There are also\r\n         some functions that we deliberately designed... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/12\/29\/a-case-for-divergence-between-matlab-and-c\/\">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],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/212"}],"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=212"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}