switch/case

OpenMatrix Language also supports a variant of this using switch/case/otherwise.

switch value
      case target1
            block1
      case target
            blockN
      otherwise
            blockOtherwise
end

The switch value is an expression to be tested against each target expression (listed as separate case statements). It must evaluate to either a scalar or a string. If the target expression for a case exactly matches the switch value, the corresponding block is run. Only one block is run for any given switch statement. If the switch value does not match any of the case targets, the blockOtherwise code is run. The otherwise block is optional.

Example 1

value = 2
switch value
      case 1
            disp('hello 1')
      case 2
            disp('hello 2')
      case 3
            disp('hello 3')
end

Example 2

value = 2
switch value
      case 1
            disp('hello 1')
      case 2
            disp('hello 2')
      case 3
            disp('hello 3')
      otherwise
            disp('well.. none of the cases')
end

Case statements may contain multiple values enclosed in {} and separated by ,’s.

Example 3

value = 2
switch value
      case {1,2,3}
            disp('1-3')
      case {4,5,6}
            disp('4-6')
end