switch

switch statement.

Syntax

  • switch(expression), case test_expression_1, statements, case test_expression_2, statements, otherwise statements, end

Description

switch statement is used to selective execute code based on the value of either scalar value or a string.

otherwise clause is optional.

Examples

demo_switch.m

function c = demo_switch(a)
 switch(a)
    case {'hello', 'world'}
      c = 'message';
    case {'red', 'green', 'blue'}
      c = 'color';
    otherwise
      c = 'not sure';
  end
end
demo_switch('hello')
demo_switch('red')
demo_switch('?')

See also

for.

History

Version
Description

1.0.0

initial version

Author

Allan CORNET

Last updated