| GNU Octave Manual by John W. Eaton Paperback (6"x9"), 324 pages, 4 figures ISBN 0954161726 RRP £19.99 ($29.99) |
10.2 The 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
octave-maintainers at bevo.che.wisc.edu. (But if you think you've
found a bug, please report it to bug-octave at bevo.che.wisc.edu.
The general form of the switch statement is
switch expression
case label
command_list
case label
command_list
...
otherwise
command_list
endswitch
-
The identifiers
switch,case,otherwise, andendswitchare now keywords. - The label may be any expression.
- Duplicate label values are not detected. The command_list corresponding to the first match will be executed.
-
You must have at least one
case label command_listclause. -
The
otherwise command_listclause is optional. -
As with all other specific
endkeywords,endswitchmay be replaced byend, but you can get better diagnostics if you use the specific forms. - Cases are exclusive, so they don't `fall through' as do the cases in the switch statement of the C language.
-
The command_list elements are not optional. Making the list
optional would have meant requiring a separator between the label and
the command list. Otherwise, things like
switch (foo) case (1) -2 ...
would produce surprising results, as wouldswitch (foo) case (1) case (2) doit (); ...particularly for C programmers. -
The implementation is simple-minded and currently offers no real
performance improvement over an equivalent
ifblock, 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.
- Built-in Variable: warn_variable_switch_label
- If the value of this variable is nonzero, Octave will print a warning if a switch label is not a constant or constant expression
| ISBN 0954161726 | GNU Octave Manual | See the print edition |