Built-in function for `switch'

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Built-in function for `switch'

Habibie Sumargo
Dear Octave developer(s),

I am wondering if octave-2.0 has a built-in function to support `switch'
statement like C language, i.e.:

        switch arg:
        {
                case 1: ...
                        break;

                case 2: ...
                        break;

                default: ...
        }

Thank you.
--
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Built-in function for `switch'

John W. Eaton-6
On 27-Feb-1997, Habibie Sumargo <[hidden email]> wrote:

| Dear Octave developer(s),

Uh, that would be me.

| I am wondering if octave-2.0 has a built-in function to support `switch'
| statement like C language, i.e.:
|
| switch arg:
| {
| case 1: ...
| break;
|
| case 2: ...
| break;
|
| default: ...
| }

No.  I have considered adding something like this, but probably with a
syntax that matches the current language a little better, maybe using
switch and endswitch keywords.

For now, you have to use if/elseif/else/endif.

I believe that the MathWorks web site mentions that MATLAB 5 supports
a switch statement.  Does anyone know what syntax it uses?

Thanks,

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Built-in function for `switch'

Guido Dietz-2
 On Fri, 28 Feb 1997 01:35:57 -0600 you, "John W. Eaton", wrote:
> No.  I have considered adding something like this, but probably with a
> syntax that matches the current language a little better, maybe using
> switch and endswitch keywords.
>
> For now, you have to use if/elseif/else/endif.
>
> I believe that the MathWorks web site mentions that MATLAB 5 supports
> a switch statement.  Does anyone know what syntax it uses?

This is an Xtract from the manual:
switch var
        case 1
                disp('1');
        case {2,3,4}                % < This is a Cell Array (new feature)
                disp('2 or 3 or 4');
        case 5
                disp('5');
        otherwise
                disp('something else');
end
NOTE: Unlike C switch does not 'fall through'!

BTW what's about multidimensional arrays ;-? Would that be too hard to
implement?

> Thanks,
> jwe
Bye

G.

------------------------------------------------------------------AIA-RWTH--
            Guido Dietz                   Email:    [hidden email]
Aerodynamisches Institut der RWTH-Aachen  WWW: http://www.aia.rwth-aachen.de
      Wuellnerstr. zw. 5 u. 7             Ftp:  ftp://ftp.aia.rwth-aachen.de
      D-52062 Aachen, Germany   Phone: +49-241-80-5426 Fax: +49-241-8888-257
----------------------------------------------------------------------------
It is always preferable to visit home with a friend.  Your parents will
not be pleased with this plan, because they want you all to themselves
and because in the presence of your friend, they will have to act like
mature human beings ...
                -- Playboy, January 1983
----------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: Built-in function for `switch'

John W. Eaton-6
I wrote:

| I believe that the MathWorks web site mentions that MATLAB 5 supports
| a switch statement.  Does anyone know what syntax it uses?

On 28-Feb-1997, Guido Dietz <[hidden email]> replied:

| This is an Xtract from the manual:
| switch var
| case 1
| disp('1');
| case {2,3,4}                % < This is a Cell Array (new feature)
| disp('2 or 3 or 4');
| case 5
| disp('5');
| otherwise
| disp('something else');
| end
| NOTE: Unlike C switch does not 'fall through'!

I just put a patch on ftp.che.wisc.edu in /pub/octave/switch-diffs
that provides a simple-minded implementation of this statement.

The new statement has the following form:

  switch EXPRESSION
    case LABEL COMMAND_LIST
    case LABEL COMMAND_LIST
    ...
    otherwise  COMMAND_LIST
  endswitch

For example:

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


NOTES:

  * 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.

Please send any comments about this to the mailing list
[hidden email].

Now for some questions I have about the MATLAB implementation of this
statement:

  * 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)?

Thanks,

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Built-in function for `switch'

Guido Dietz-2

Puhh, very promt implementation of switch. Your support is better than any
commercial one!

 On Fri, 28 Feb 1997 16:26:33 -0600 you, "John W. Eaton", wrote:
> Now for some questions I have about the MATLAB implementation of this
> statement:
I didn't get the right passcode for running Matlab 5 yet. So the following
infos are all from the manual, as far as I understood that right ;-)

>   * Does it require constant labels?
Seems so, look below ...

>   * Do the labels have to be unique?
No, but the second occurence will have no effect :-), see below ...

>   * Are the command lists optional?
Hmm, I will test it ...

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

>   * Are the `case' and `otherwise' clauses optional (i.e., is `switch end'
>     a valid statement)?
"Any number of case groups" is allowed, I 'll test the zero ...

They say:
switch *expression* (scalar or string)
  case value1
        *statements*
  case value2
        *statements*
  .
  .
  .
  otherwise
        *statements*
end

o The word swtitch followed by a scaler or string expression to evaluate.
o A case group consists of the word case followed by a possible value for
  expression, all on a single line! switch executes the following statements
  until MATLAB encounters the next case statement or the otherwise statement.
  Only the first matching case is executed!

G.

------------------------------------------------------------------AIA-RWTH--
            Guido Dietz                   Email:    [hidden email]
Aerodynamisches Institut der RWTH-Aachen  WWW: http://www.aia.rwth-aachen.de
      Wuellnerstr. zw. 5 u. 7             Ftp:  ftp://ftp.aia.rwth-aachen.de
      D-52062 Aachen, Germany   Phone: +49-241-80-5426 Fax: +49-241-8888-257
----------------------------------------------------------------------------
        Your home electrical system is basically a bunch of wires that
bring electricity into your home and take if back out before it has a
chance to kill you.  This is called a "circuit".  The most common home
electrical problem is when the circuit is broken by a "circuit
breaker"; this causes the electricity to back up in one of the wires
until it bursts out of an outlet in the form of sparks, which can
damage your carpet.  The best way to avoid broken circuits is to change
your fuses regularly.
        Another common problem is that the lights flicker.  This
sometimes means that your electrical system is inadequate, but more
often it means that your home is possessed by demons, in which case
you'll need to get a caulking gun and some caulking.  If you're not
sure whether your house is possessed, see "The Amityville Horror", a
fine documentary film based on an actual book.  Or call in a licensed
electrician, who is trained to spot the signs of demonic possession,
such as blood coming down the stairs, enormous cats on the dinette
table, etc.
                -- Dave Barry, "The Taming of the Screw"
----------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: Built-in function for `switch'

John W. Eaton-6
In reply to this post by Guido Dietz-2
On 28-Feb-1997, Guido Dietz <[hidden email]> wrote:

| BTW what's about multidimensional arrays ;-? Would that be too hard to
| implement?

Adding support for them is definitely on the list of things to do, but
the list is long and I don't know when I might be able to work on this
project.

If you want to see support for multidimensional arrays sooner, add
your own user-defined type to support them and contribute it.

Thanks,

jwe