'break' statements to jump among switch cases: how to?

Discussion in 'Java' started by z-man, Oct 5, 2006.

  1. z-man

    z-man Guest

    Hi all

    Is there a legal way to use break statements to jump from one switch
    case to another (see below)?

    Thanks!

    switch(mode)
    {
    case Partial:
    if(reader == null)
    break Full;

    ...
    break;
    case Full:
    ...
    break;
    }
     
    z-man, Oct 5, 2006
    #1
    1. Advertising

  2. z-man

    Chris Brat Guest

    No.

    The break lable; jump is restricted to loops AFAIK.
     
    Chris Brat, Oct 5, 2006
    #2
    1. Advertising

  3. z-man wrote:
    > Is there a legal way to use break statements to jump from one switch
    > case to another (see below)?
    >
    > Thanks!
    >
    > switch(mode)
    > {
    > case Partial:
    > if(reader == null)
    > break Full;
    >
    > ...
    > break;
    > case Full:
    > ...
    > break;
    > }


    The only way I know is: run from one case into the next case by *not*
    doing a "break". The code below does what you wanted in your code above.

    switch(mode)
    {
    case Partial:
    if(reader != null)
    {
    ...
    break;
    }
    /*FALL THROUGH*/
    case Full:
    ...
    break;
    }

    But you should do such things only when absolutely needed. At least put
    a fat comment there, stating that you omitted the "break" on purpose and
    not by accident.

    --
    Thomas
     
    Thomas Fritsch, Oct 5, 2006
    #3
  4. z-man

    z-man Guest

    On 10/05/2006 03:31 PM, Chris Brat wrote:
    > No.
    >
    > The break lable; jump is restricted to loops AFAIK.


    If it works this way, I think that the Java switch statement flow is
    unconsistent, because of the fall-through behavior (that is nothing but
    a degenerate, implicit jump to a subsequent case statement!).

    IMHO, fall-through is too much limitative: adding
    break-to-labeled-statement statements to the switch syntax would be a
    useful and elegant extension to the current simple break statement (the
    same purpose of the ugly-but-effective 'go to' statement of C#). To be
    used wisely and sparingly, obviously!
     
    z-man, Oct 5, 2006
    #4
  5. z-man

    z-man Guest

    On 10/05/2006 04:01 PM, Thomas Fritsch wrote:
    > z-man wrote:
    >> Is there a legal way to use break statements to jump from one switch
    >> case to another (see below)?
    >>
    >> Thanks!
    >>
    >> switch(mode)
    >> {
    >> case Partial:
    >> if(reader == null)
    >> break Full;
    >>
    >> ...
    >> break;
    >> case Full:
    >> ...
    >> break;
    >> }

    >
    > The only way I know is: run from one case into the next case by *not*
    > doing a "break". The code below does what you wanted in your code above.
    >
    > switch(mode)
    > {
    > case Partial:
    > if(reader != null)
    > {
    > ...
    > break;
    > }
    > /*FALL THROUGH*/
    > case Full:
    > ...
    > break;
    > }
    >
    > But you should do such things only when absolutely needed. At least put
    > a fat comment there, stating that you omitted the "break" on purpose and
    > not by accident.


    Thanks, Thomas.

    I wrote the following post to Chris just before reading your comment:

    "If it works this way, I think that the Java switch statement flow is
    unconsistent, because of the fall-through behavior (that is nothing but
    a degenerate, implicit jump to a subsequent case statement!).

    IMHO, fall-through is too much limitative: adding
    break-to-labeled-statement statements to the switch syntax would be a
    useful and elegant extension to the current simple break statement (the
    same purpose of the ugly-but-effective 'go to' statement of C#). To be
    used wisely and sparingly, obviously!"
     
    z-man, Oct 5, 2006
    #5
  6. z-man

    Chris Brat Guest

    > If it works this way, I think that the Java switch statement flow is
    > unconsistent, because of the fall-through behavior (that is nothing but
    > a degenerate, implicit jump to a subsequent case statement!).


    No it is consistent - You are guaranteed that a switch statement will
    execute from top-to-bottom and fallthrough will occur untill the switch
    ends (either by reaching the end of the switch or until a break
    statement is executed).

    > IMHO, fall-through is too much limitative: adding
    > break-to-labeled-statement statements to the switch syntax would be a
    > useful and elegant extension to the current simple break statement (the
    > same purpose of the ugly-but-effective 'go to' statement of C#).


    This is very much like the 'goto' which IMHO is an ingredient for
    spagetti code

    > To be used wisely and sparingly, obviously!

    famous last words
     
    Chris Brat, Oct 5, 2006
    #6
  7. z-man wrote:
    > Hi all
    >
    > Is there a legal way to use break statements to jump from one switch
    > case to another (see below)?
    >
    > Thanks!
    >
    > switch(mode)
    > {
    > case Partial:
    > if(reader == null)
    > break Full;
    >
    > ...
    > break;
    > case Full:
    > ...
    > break;
    > }


    Not as arbitrary jumps among cases. See
    http://www.acm.org/classics/oct95/ for the main arguments against doing it.

    Possible solutions include running the switch multiple times, changing
    mode as needed, or putting the common work in a method, called in each
    situation in which it is needed.

    Patricia
     
    Patricia Shanahan, Oct 5, 2006
    #7
  8. "Chris Brat" <> writes:

    > This is very much like the 'goto' which IMHO is an ingredient for
    > spagetti code


    And hence why C# has it :)

    (There you need to end every case block with either a break or a "goto
    case label", there is no fallthrough.)
     
    Tor Iver Wilhelmsen, Oct 5, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Parkoff

    65536 Switch Cases

    Bryan Parkoff, Sep 16, 2004, in forum: C++
    Replies:
    5
    Views:
    463
    Ivan Vecerina
    Sep 17, 2004
  2. He Shiming

    Hundreds of cases in a switch, optimization?

    He Shiming, Jun 19, 2005, in forum: C Programming
    Replies:
    65
    Views:
    1,740
    Christian Bau
    Jun 24, 2005
  3. Lynn McGuire

    maximum cases in a switch ?

    Lynn McGuire, Aug 9, 2011, in forum: C++
    Replies:
    67
    Views:
    2,401
    James Kanze
    Aug 22, 2011
  4. Ronny
    Replies:
    6
    Views:
    137
  5. Eadwine Rose
    Replies:
    2
    Views:
    221
    Eadwine Rose
    Oct 15, 2006
Loading...

Share This Page