Next: The while Statement, Previous: The if Statement, Up: Statements
switch Statement
The switch statement was introduced in Octave 2.0.5. It should
be considered experimental, and details of the implementation may change
slightly in future versions of Octave. If you have comments or would
like to share your experiences in trying to use this new command in real
programs, please send them to maintainers@octave.org. (But if
you think you've found a bug, please report it to
bug@octave.org.
The general form of the switch statement is
switch expression
case label
command_list
case label
command_list
...
otherwise
command_list
endswitch
switch, case, otherwise, and
endswitch are now keywords.
case label command_list
clause.
otherwise command_list clause is optional.
end keywords, endswitch may be
replaced by end, but you can get better diagnostics if you use
the specific forms.
switch (foo)
case (1) -2
...
would produce surprising results, as would
switch (foo)
case (1)
case (2)
doit ();
...
particularly for C programmers.
if block, even if all
the labels are integer constants. Perhaps a future variation on this
could detect all constant integer labels and improve performance by
using a jump table.