相关文章推荐

Sie verfolgen jetzt diese Frage

  • Aktualisierungen können Sie in Ihrem Feed verfolgter Inhalte sehen.
  • Je nach Ihren Kommunikationseinstellungen können Sie auch E-Mails erhalten.
  • 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)?
    % 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
    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.

    Community Treasure Hunt

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

    Start Hunting!
     
    推荐文章