[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

multiple case components in switch-case statement

From: John W. Eaton
Subject: multiple case components in switch-case statement
Date: Thu, 22 Oct 1998 10:09:15 -0500 (CDT)

On 22-Oct-1998, Ferdinand Schinagl <address@hidden> wrote:

| I found out that matlab's sytax for the switch
| statement enables checking of multiple alternatives
| within a single case directive:
|         SWITCH switch_expr
|           CASE case_expr,
|             statement, ..., statement
|           CASE {case_expr1, case_expr2, case_expr3,...}
|             statement, ..., statement
|          ...
|           OTHERWISE,
|             statement, ..., statement
|         END
| To my knowledge, however, octave does not support
| things like
| str = "asdf"
| .
| # some code
| .
| switch str
| case "a"
|   # do thing 1
| case "b"
|   # do something
| case "asdf"
|   # do it!
| endswitch
| Is this feature available in octave version 2.0.13,
| or planned for 2.1.x versions?

Changes for this are already integrated in the 2.1..x `bleeding-edge'
sources that are available on  Octave's version does
not yet support the 

   CASE {case_expr1, case_expr2, ...}

form though.  Octave 2.1.x does have a list data type, so maybe it
will eventually support this syntax.  (BTW, for those who are
interested, there should be an update to the 2.1.x sources soon.)

There are also some diffs at
Here is a copy of the comments at the top of that file:

  The diffs below provide a simple-minded implementation of a switch
  statement for Octave 2.0.x.

  The new statement has the following form:

    switch EXPRESSION
      otherwise  COMMAND_LIST

  For example:

    switch (x)
      case 1
        do_one ();
      case foo
        do_foo ();
        do_default ();


    * The identifiers `switch', `case', `otherwise', and `endswitch' are
      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_LIST' clause.

    * The `otherwise COMMAND_LIST' clause is optional.

    * As with all other specific `end' keywords, `endswitch' may be
      replaced by `end', 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 would

        switch (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 if/elseif/else/endif
      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.

  MATLAB 5 also has a similar switch statement, but I'm not sure exactly
  how it works.  Can anyone tell me the answers to the following

    * Does it require constant labels?

    * Do the labels have to be unique?

    * Are the command lists optional?

    * Is a separator (newline, comma, or semicolon) required between the
      case labels and the command lists?

    * Are the case and otherwise clauses optional (i.e., is `switch end'
      a valid statement?

  Please send any comments about this to the mailing list



reply via email to

[Prev in Thread] Current Thread [Next in Thread]