i think in matlab there is no difference between switch and many if/elseif statements. the switch is also known in many other programming languages, but behaves a bit differently than in matlab. in matlab the corresponding case is executed and the code continues behind the block, in other languages the switch block is exited only if there is a break at the end of the case, otherwise other cases would be tested too. i hope i remebered correctly, it has been some time since my last c++ session
if/elseif/else
is quite similar to
switch/case/otherwise
. Both provide a default action when no conditions are met (
else & otherwise
) and you're encouraged to use those options. In Matlab, both sets of tests stop execution once a condition is met (in some other languages all cases are executed when there are multiple case matches). I doubt either approach has a computationally efficient beneifit over the other.
Here are some of their differences.
-
When there are multple conditions,
switch/case/otherwise
is often the more organized approach resulting in more readable code. It often reduces the need to use operators and functions to test each condition. For example, when testing string matches,
if/elseif/else
requires using a string comparson function such as
strcmp/i
,
ismember
, etc or an operator (
==
) for strings while a
switch/case/otherwise
just uses a string for each case and if case sensitivity should be ignored, the switch statement could use
upper/lower
to force the sting to upper or lower case while each case-condition contains the same letter-case.
-
switch/case/otherwise
is easier to lay out when conditions all rely on the same class such as testing matches to strings, categories,enumerated values but
if/elseif/else
can be easier to write and read with mixed classes or complex condition tests such as ranges of values. Note that both sets of tests can use expressions or call functions in Matlab but it can become unpleasent to design and read using
switch/case/otherwise
.
Which is more readable for you (ignore that these can likely be vectorized)?
error(
'Country not listed: %s'
, country)
if
strcmpi(country,
'Albania'
)
elseif
strcmpi(country,
'Bosnia'
)
elseif
strcmpi(country,
'Croatia'
)
elseif
strcmpi(country,
'Slovenia'
)
error(
'Country not listed: %s'
, country)
Also see
They're pretty much the same, just slightly different syntax as to how to get the condition. Switch requires an extra line of code than if but that's no big deal.
Use which ever one you think makes it easier to read your code. If the condition needs to be made up from multiple tests/operations, then it might be better in an if. switch is best for situations where the condition is just a single value (number, string), or cell array of several such values.
value = (blah + fubar) * snafu
if
(blah + fubar) * snafu == 1
elseif
(blah + fubar) * snafu == 2 && (someOtherVariable == 3)
My two cents worth, for what it's worth.
Find the treasures in MATLAB Central and discover how the community can help you!