MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
MathWorks
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out

Videos and Webinars

  • MathWorks
  • Videos
  • Videos Home
  • Search
  • Videos Home
  • Search
  • Contact sales
  • Trial software
  Register to watch video
  • Description
  • Full Transcript
  • Related Resources

Switch Case vs. If Elseif

Doug Hull, MathWorks

(Originally posted on Doug's MATLAB Video Tutorials blog.)

This three minute video takes a look at the "Switch case" flow control statement and contrasts it with the more familiar "If elseif" flow control statement. Often times, people will use an "If elseif" statement where a "Switch case" statement is going to be cleaner and easier to understand and maintain.

Other videos have been gathered here: https://blogs.mathworks.com/pick/category/video/ Other MATLAB Basics posts have been gathered here: https://blogs.mathworks.com/pick/category/matlab-basics/ As a cool little post script: At most places you celebrate the 100th something or other, but since this is a software company I feel I should celebrate the 64th video (2^6) and as I post this I see that it is the 18128th file on the File Exchange capturing the next power of two in the last three digits.

Recorded: 2 Jan 2008

Today in MATLAB Basics, we're going to discuss the use of switch case statements as a cleaner, more maintainable way of doing something like this, that you often see done with else/if statements. Notice in this if/else statement, what we have is someone is creating a val equal to 3 variable, and then checking with a bunch of if/elses to find out what the value is. And then displaying the correct words.

How would this look if it were done as a switch? I think this is more clean and easy to understand doing it this way. Notice there's just a lot less characters on screen when we do it this way. Basically, what we're doing is saying, I want to switch through these cases as if it was a switch yard at a train depot or something like that. And so we're going to be switching based on the variable val.

If the case is 1, that is if val equals 1, display 1. Otherwise, we're going to try case two, case three. And if none of those were correct, then we're just going to say otherwise and do something else. That's how the switch case works.

And if we run it here, we can see that it indeed did find the correct value. If we come here and put our value as 8 and run it again, we can see that it just goes through and says something else. One of the other advantages of switch case in where it's most often actually used is with strings.

Let's take a look here with strings where we can just say, my string is equal to alpha. And then we're going to switch through that railroad switch yard based on the different cases. Now, maybe we're interested in doing one thing if it's alpha or bravo. And then something specific if it's Charlie. And then otherwise we just say unknown.

Let's give that a shot running a F5. And we can see that works also, that either alpha or bravo would work just fine there. OK. This should show you why using a switch case statement can be cleaner than its equivalent in an if-then-else statement. Thank you.

Related Products

  • MATLAB
Related Information
MATLAB Video Blog

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

1:46
Using Otherwise to Throw Errors in a Switch Case

Related Videos:

39:11
Predictive Modeling Using Machine Learning - A Mining Case...
24:01
Best Practices and Lessons Learned During Test Case...
29:18
Modeling and Simulation with MATLAB: A Case Study in...
19:38
Stateflow Design Patterns: Switch-Case and Reusable Flow...

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Contact sales
  • Trial software

Explore Products

  • MATLAB
  • Simulink
  • Student Software
  • Hardware Support
  • File Exchange

Try or Buy

  • Downloads
  • Trial Software
  • Contact Sales
  • Pricing and Licensing
  • How to Buy

Learn to Use

  • Documentation
  • Tutorials
  • Examples
  • Videos and Webinars
  • Training

Get Support

  • Installation Help
  • Answers
  • Consulting
  • License Center
  • Contact Support

About MathWorks

  • Careers
  • Newsroom
  • Social Mission
  • Contact Sales
  • About MathWorks

MathWorks

Accelerating the pace of engineering and science

MathWorks is the leading developer of mathematical computing software for engineers and scientists.

Discover…

  • Select a Web Site United States
  • Patents
  • Trademarks
  • Privacy Policy
  • Preventing Piracy
  • Application Status

© 1994-2021 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Join the conversation

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic.  By continuing to use this website, you consent to our use of cookies.  Please see our Privacy Policy to learn more about cookies and how to change your settings.