Skip to main content
Library homepage
 
Engineering LibreTexts

7.9: 'switch' Logic

  • Page ID
    84969
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This part is by Troy Siemers.

    We review the if\elseif\else construct and introduce the switch\case\otherwise construct in this section.

    Suppose we want to have part of our program run only under certain conditions. For this, we use an if\else structure. The basic format of this structure is:

    if (put condition(s) here)(put calculations here to be run if the conditions are met)
    elseif (other condition(s))(calculations that will run under the new conditions)(more elseif statements, if desired)
    else (calculations run if none of the previous conditions are met)
    end

    Example 1: Using if

    Let’s write a script M-file that lets a user input a number and then displays if that number is less than 5, between 5 and 10 (inclusive), or greater than 10.

    number = input('Input a number ');
    if (number < 5)
    disp('Your number is less than 5.')
    elseif ((number >=5) && (number <= 10))
    disp('Your number is between 5 and 10.')
    else
    disp('Your number is greater than 10.')
    end

    Try running this program using various numbers for input.

    The switch\case structure is similar to the if structure, but has a few advantages. First of all it is easier to read and second, it is better if you are comparing strings (of possibly different lengths). The basic format is:

    switch (expression to test)
    case (case condition)
    (output in that case)
    case (case condition)
    (output in that case)

    ...(more cases)
    otherwise
    (do this if no cases are met)

    end

    Example 2: Using switch

    Let’s check to see if a cadet is in his\her first two years at VMI.

    Year = 'second class';

    switch Year
    case {'fourth class','third class'}
    disp('You are in the first two years.')
    case {'second class','first class'}
    disp('You are in the last two years.')
    otherwise
    disp('You must be a 5th year.')

    end

    The cases are grouped by curly brackets so that a case will be satisfied if the value of Year is any of the values in a specific case. Once this code is executed, the switch command will look at the value of Year and the output should be

    You are in the last two years.

    .

    This part is by Carey Smith

    Example \(\PageIndex{3}\) Switch_Major.m

    This script uses the menu() function which displays a list of choices. The user clicks on one of the menu items. This converts the selection to a number stored in the variable Major.

    The switch structure uses the Major variable value to determine a message to display.

    % Switch_Major.m
    Major = menu('Select a Major: ', ...
    'Physics', 'Engineering', 'Math', 'Chemistry', ...
    'History','Psychology', ...
    'English', 'Art', 'Buisiness')
    switch Major
    case {1,2,3,4}
    disp('That is a STEM major')
    case {5,6}
    disp('That is a Social Science major')
    otherwise
    disp('That is neither a STEM nor a Social Science major')
    end

    Solution

    Add example text here.

    .

    Exercise \(\PageIndex{1}\) Switch Shop Hours

    Review the Switch_Major.m code above, then due this assignment.

    Create a switch script named Switch_Shop_Hours_YourName.m

    The user will chose a day of the week, and the script will display the hours a shop is open
    or a message that the shop is closed.

    Use the following menu function to allow the user to select a day of the week:
    day = menu('Select a day of the week: ',...
    'Sun', 'Mon', 'Tues','Weds', ...
    'Thurs', 'Fri', 'Sat')

    Then write switch logic using "day" as the switch variable.
    [Day will be an integer from 1 to 7]
    For the cases that the day is 1 or 2 (Sun or Mon), display:
    'The shop is closed'
    For the cases that the day is 3, 4, or 5 (Tues, Weds, or Thurs), display:
    'The shop is open 8am to 5:30pm'
    For the case that the day is 6 (Fri), display:
    'The shop is open 8am to 7:00pm'
    For the case that the day is 7 (Sat), display:
    'The shop is open 8am to noon'

    Answer

    Add texts here. Do not delete this text first.

    .

    This is a video on the switch statement:

    https://www.youtube.com/watch?v=zqGJE2WGHq4

    Switch Statement in Matlab, Learnrope Tutorials

    .


    This page titled 7.9: 'switch' Logic is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.