MATLAB Answers

Difference between switch and if

79 views (last 30 days)
Bob Thompson
Bob Thompson on 21 Apr 2021
Commented: Bob Thompson on 21 Apr 2021
I just ran across switch being used in a code and am wonder how it differs from if. The use of switch may not have been well set up in the code I was looking at, because they look generally the same. I did not read the documentation in great detail, just enough to get an idea of what switch does.
  2 Comments
Bob Thompson
Bob Thompson on 21 Apr 2021
Thanks, it's good to know I should keep an eye out for this in other languages.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 21 Apr 2021
Edited: Adam Danz on 21 Apr 2021
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)?
% Example 1
country = 'Croatia';
switch lower(country)
case 'albania'
y = ...
case 'bosnia'
y = ...
case 'croatia'
y = ...
case 'slovenia'
y = ...
otherwise
error('Country not listed: %s', country)
end
if strcmpi(country,'Albania')
y = ...
elseif strcmpi(country,'Bosnia')
y = ...
elseif strcmpi(country,'Croatia')
y = ...
elseif strcmpi(country,'Slovenia')
y = ...
else
error('Country not listed: %s', country)
end
x = pi;
switch true
case x>0 && x<=1
y = ...
case x>1 && x<=2
y = ...
case x>2 && x<=3
y = ...
case x>3 && x<=4
y = ...
otherwise
y = . . .
end
if x>0 && x<=1
y = ...
elseif x>1 && x<=2
y = ...
elseif x>2 && x<=3
y = ...
elseif x>3 && x<=4
y = ...
else
y = . . .
end
Also see
  1 Comment
Bob Thompson
Bob Thompson on 21 Apr 2021
Thanks, this was helpful and provided a good level of depth.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 21 Apr 2021
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.
switch(value)
case 1
% code
otherwise
% code
end % 4 lines of code
if value == 1
% code
else
%code
end % 3 lines of code
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
switch(value)
case 1
% code
case {2, 3}
% code
otherwise
% code
end
if (blah + fubar) * snafu == 1
% code
elseif (blah + fubar) * snafu == 2 && (someOtherVariable == 3) % Hard to do with "case"
% code
end
My two cents worth, for what it's worth.
  1 Comment
Bob Thompson
Bob Thompson on 21 Apr 2021
Thanks, I appreciate the different perspective from how others described it.

Sign in to comment.

Tags

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!