switch

Programming

Multi-way branch. Compares the switch expression against each case value. Executes the first matching case block. The otherwise block runs if no case matches. Unlike C, no fall-through between cases.

Syntax

switch expr
  case val1
    ...
  case val2
    ...
  otherwise
    ...
end

Examples

method = 'euler';
switch method
  case 'euler'
    disp('First order')
  case 'rk4'
    disp('Fourth order')
  otherwise
    disp('Unknown')
end
▶ Run

See Also

Try SimLab — Free MATLAB® Alternative

466 functions. Runs in your browser. No install.

Open SimLab

Stay Updated

Get notified about new simulations and tools. We send 1-2 emails per month.