How to break a while loop inside a switch statement?

Discussion in 'Java' started by howachen@gmail.com, Jul 9, 2006.

  1. Guest

    while ( ... ) {

    switch (test) {

    case 'e':
    // HOW TO BREAK THE WHILE LOOP ? I CAN'T USE THE "BREAK" HERE
    break;

    default:
    ...

    break;
    }
    }
    , Jul 9, 2006
    #1
    1. Advertising

  2. Andrew Tyson Guest

    wrote:
    > while ( ... ) {
    >
    > switch (test) {
    >
    > case 'e':
    > // HOW TO BREAK THE WHILE LOOP ? I CAN'T USE THE "BREAK" HERE
    > break;
    >
    > default:
    > ...
    >
    > break;
    > }
    > }
    >

    FOO_BAR : while() {
    switch (blah)
    case 'e':
    break FOO_BAR;
    ...
    }
    Andrew Tyson, Jul 9, 2006
    #2
    1. Advertising

  3. "Andrew Tyson" <> wrote in message
    news:44b098e1$0$22362$...
    > wrote:
    >> while ( ... ) {
    >>
    >> switch (test) {
    >>
    >> case 'e':
    >> // HOW TO BREAK THE WHILE LOOP ? I CAN'T USE THE "BREAK" HERE
    >> break;
    >>
    >> default:
    >> ...
    >>
    >> break;
    >> }
    >> }
    >>

    > FOO_BAR : while() {
    > switch (blah)
    > case 'e':
    > break FOO_BAR;


    HUH? What's this?

    Is this for real?

    If it is, I would like to take a second to rant about all the damn times I
    have made worthless boolean variables to hold exit, fail, "error" or bad
    values simply as an exit condition for a nested loop.

    And I am left to wonder . . . how many other things do I not know that I
    don't know?

    --
    LTP

    :)
    Luc The Perverse, Jul 9, 2006
    #3

  4. > HUH? What's this?
    >
    > Is this for real?
    >
    > If it is, I would like to take a second to rant about all the damn times I
    > have made worthless boolean variables to hold exit, fail, "error" or bad
    > values simply as an exit condition for a nested loop.
    >



    It is for real.

    I prefer, and recommend, boolean variables anyway.

    Cheers.
    opalinski from opalpaweb, Jul 9, 2006
    #4
  5. Timo Stamm Guest

    Luc The Perverse schrieb:
    >> FOO_BAR : while() {
    >> switch (blah)
    >> case 'e':
    >> break FOO_BAR;

    >
    > HUH? What's this?


    It's a label. You can use labels for loop constructs:

    label: while(true) {}
    label: for (;;);
    label: do {} while (true);

    And for blocks:

    a: {
    // ...
    b: {
    // ...
    break a;
    }
    // ...
    }

    Blocks are quite interesting. You can use them to structure long method
    bodies (local variables are block-local) and you can even nest them and
    use labels to exit them.

    But I haven't found any practical use for them yet. I guess it's always
    better to use a separate method instead of a block.


    Timo
    Timo Stamm, Jul 9, 2006
    #5
  6. PSUnderwood Guest

    Yup. It's a labeled break, and it's a wonderful thing. You can do a
    similar thing with continue, if you're so inclined.

    Have a nice rant.

    Regards,
    Paul

    Luc The Perverse wrote:
    > "Andrew Tyson" <> wrote in message
    > news:44b098e1$0$22362$...
    >
    >> wrote:
    >>
    >>>while ( ... ) {
    >>>
    >>>switch (test) {
    >>>
    >>>case 'e':
    >>>// HOW TO BREAK THE WHILE LOOP ? I CAN'T USE THE "BREAK" HERE
    >>>break;
    >>>
    >>>default:
    >>>...
    >>>
    >>>break;
    >>>}
    >>>}
    >>>

    >>
    >>FOO_BAR : while() {
    >>switch (blah)
    >> case 'e':
    >>break FOO_BAR;

    >
    >
    > HUH? What's this?
    >
    > Is this for real?
    >
    > If it is, I would like to take a second to rant about all the damn times I
    > have made worthless boolean variables to hold exit, fail, "error" or bad
    > values simply as an exit condition for a nested loop.
    >
    > And I am left to wonder . . . how many other things do I not know that I
    > don't know?
    >
    > --
    > LTP
    >
    > :)
    >
    >
    PSUnderwood, Jul 9, 2006
    #6
  7. Luc The Perverse wrote:
    > "Andrew Tyson" <> wrote in message
    > news:44b098e1$0$22362$...
    >> wrote:
    >>> while ( ... ) {
    >>>
    >>> switch (test) {
    >>>
    >>> case 'e':
    >>> // HOW TO BREAK THE WHILE LOOP ? I CAN'T USE THE "BREAK" HERE
    >>> break;
    >>>
    >>> default:
    >>> ...
    >>>
    >>> break;
    >>> }
    >>> }
    >>>

    >> FOO_BAR : while() {
    >> switch (blah)
    >> case 'e':
    >> break FOO_BAR;

    >
    > HUH? What's this?
    >
    > Is this for real?
    >
    > If it is, I would like to take a second to rant about all the damn times I
    > have made worthless boolean variables to hold exit, fail, "error" or bad
    > values simply as an exit condition for a nested loop.


    The question of which is more readable, labeled break or boolean
    controls, depends on the situation and is highly arguable. I'm aware of
    the labeled break, but usually prefer a boolean. In particular, the
    boolean makes it easy to self-document the reason for stopping:

    boolean widgetFound = false;
    while(!widgetFound){
    switch(blah)
    case e:
    widgetFound = true;
    break;
    ....

    > And I am left to wonder . . . how many other things do I not know that I
    > don't know?


    That is one of the reasons I hang out in this newsgroup, and often read
    the other responses even if I think I know the answer.

    Patricia
    Patricia Shanahan, Jul 9, 2006
    #7
  8. Juha Laiho Guest

    "Luc The Perverse" <> said:
    >"Andrew Tyson" <> wrote in message
    >news:44b098e1$0$22362$...
    >> wrote:
    >>> while ( ... ) {
    >>> switch (test) {
    >>> case 'e':
    >>> // HOW TO BREAK THE WHILE LOOP ? I CAN'T USE THE "BREAK" HERE
    >>> break;

    ....
    >> FOO_BAR : while() {
    >> switch (blah)
    >> case 'e':
    >> break FOO_BAR;

    >
    >HUH? What's this?


    As others have told, a labeled statement, and break from a labeled block.

    >Is this for real?


    This should be a good place to start your research:
    http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.7

    >And I am left to wonder . . . how many other things do I not know that I
    >don't know?


    You'll find out at least some of that by reading through the JLS (something
    I should do myself, too..).
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Jul 9, 2006
    #8
  9. Chris Uppal Guest

    Luc The Perverse wrote:

    > If it is, I would like to take a second to rant about all the damn times I
    > have made worthless boolean variables to hold exit, fail, "error" or bad
    > values simply as an exit condition for a nested loop.


    Personally, I have never used the feature, and have only ever seen it used very
    rarely. Nor do I (ever) use boolean flags to control loop exit conditions and
    the like.

    IMO both should be avoided except in very special circumstances.

    -- chris
    Chris Uppal, Jul 10, 2006
    #9
  10. "Chris Uppal" <-THIS.org> wrote in message
    news:44b23c3b$1$782$...
    > Luc The Perverse wrote:
    >
    >> If it is, I would like to take a second to rant about all the damn times
    >> I
    >> have made worthless boolean variables to hold exit, fail, "error" or bad
    >> values simply as an exit condition for a nested loop.

    >
    > Personally, I have never used the feature, and have only ever seen it used
    > very
    > rarely. Nor do I (ever) use boolean flags to control loop exit conditions
    > and
    > the like.
    >
    > IMO both should be avoided except in very special circumstances.
    >
    > -- chris
    >
    >
    >


    You have never needed out of a nested loop?

    --
    LTP

    :)
    Luc The Perverse, Jul 10, 2006
    #10
  11. Chris Uppal Guest

    Luc The Perverse wrote:

    > You have never needed out of a nested loop?


    Not that I remember, no. Single level break and continue, plus return have
    always been enough to me without spoiling the code structure. And I've worked
    in lots of domains, so I don't think that it's just a case of "it's not needed
    for <such and such> code".

    That is, of course, only as far as I can remember. I'm not claiming that there
    may not have been exception circumstances when I would have used a targeted
    break if I had one available, but I don't think they've been at all common. I
    grew up with C where there is no such feature, so maybe I've just learned to
    structure my code with little reliance on syntactically nested loops (which I
    find confusing anyway).

    -- chris
    Chris Uppal, Jul 10, 2006
    #11
  12. Luc The Perverse wrote:
    > "Chris Uppal" <-THIS.org> wrote in message
    > news:44b23c3b$1$782$...
    >> Luc The Perverse wrote:
    >>
    >>> If it is, I would like to take a second to rant about all the damn times
    >>> I
    >>> have made worthless boolean variables to hold exit, fail, "error" or bad
    >>> values simply as an exit condition for a nested loop.

    >> Personally, I have never used the feature, and have only ever seen it used
    >> very
    >> rarely. Nor do I (ever) use boolean flags to control loop exit conditions
    >> and
    >> the like.
    >>
    >> IMO both should be avoided except in very special circumstances.
    >>
    >> -- chris
    >>
    >>
    >>

    >
    > You have never needed out of a nested loop?
    >
    > --
    > LTP
    >
    > :)
    >
    >


    No you dont have to....

    By 'separating Query from Modification'
    http://www.refactoring.com/catalog/separateQueryFromModifier.html

    and/or combined with

    'Spilt loop'
    http://www.refactoring.com/catalog/splitLoop.html
    AndrewMcDonagh, Jul 10, 2006
    #12
  13. lordy Guest

    On 2006-07-10, AndrewMcDonagh <> wrote:
    > Luc The Perverse wrote:
    >>
    >> You have never needed out of a nested loop?
    >>

    >
    > No you dont have to....
    >
    > By 'separating Query from Modification'
    > http://www.refactoring.com/catalog/separateQueryFromModifier.html
    >
    > and/or combined with
    >
    > 'Spilt loop'
    > http://www.refactoring.com/catalog/splitLoop.html


    There are times when a simple nested loop of 2-4ish levels is easier to
    read and maintain than having each loop level in its own method. The
    latter might often require extra fields or parameters to be introduced
    to pass state between the loop levels. More readable to just have nested
    loops. Not always but mostly IMO.

    Also nested loops may occur where there is a lot of CPU intensive
    processing. In such a case it would be bad to introduce extra method
    calls, and replace local variables with fields and/or parameters just
    because you didn't want to do 'break LABEL'

    Lordy
    lordy, Jul 11, 2006
    #13
  14. "lordy" <> wrote in message
    news:...
    > On 2006-07-10, AndrewMcDonagh <> wrote:
    >> Luc The Perverse wrote:
    >>>
    >>> You have never needed out of a nested loop?
    >>>

    >>
    >> No you dont have to....
    >>
    >> By 'separating Query from Modification'
    >> http://www.refactoring.com/catalog/separateQueryFromModifier.html
    >>
    >> and/or combined with
    >>
    >> 'Spilt loop'
    >> http://www.refactoring.com/catalog/splitLoop.html

    >
    > There are times when a simple nested loop of 2-4ish levels is easier to
    > read and maintain than having each loop level in its own method. The
    > latter might often require extra fields or parameters to be introduced
    > to pass state between the loop levels. More readable to just have nested
    > loops. Not always but mostly IMO.
    >
    > Also nested loops may occur where there is a lot of CPU intensive
    > processing. In such a case it would be bad to introduce extra method
    > calls, and replace local variables with fields and/or parameters just
    > because you didn't want to do 'break LABEL'
    >
    > Lordy


    I HAVE used try catch blocks to exit before - but the overhead of such a
    thing is absurd (AFAIK).

    --
    LTP

    :)
    Luc The Perverse, Jul 11, 2006
    #14
  15. EJP Guest

    Luc The Perverse wrote:

    > You have never needed out of a nested loop?


    Back when I was writing interpreters and cycles were critical we did it
    like this:

    while (condition)
    {
    // ...
    switch (opcode)
    {
    case 0:
    // ..
    continue;
    case 1:
    // ..
    continue:
    case 2:
    if (...)
    break;
    continue;
    // ...
    } // switch
    // stuff at end of switch to be reached only via 'break'
    } // while
    EJP, Jul 11, 2006
    #15
  16. Chris Uppal Guest

    EJP wrote:

    > Back when I was writing interpreters and cycles were critical we did it
    > like this:
    >
    > while (condition)
    > {
    > // ...
    > switch (opcode)
    > {
    > case 0:
    > // ..
    > continue;
    > case 1:
    > // ..
    > continue:
    > case 2:
    > if (...)
    > break;
    > continue;
    > // ...
    > } // switch
    > // stuff at end of switch to be reached only via 'break'
    > } // while


    I've used almost exactly that structure for that purpose too -- the only
    difference was that I had a for (;;) loop around it rather than a condition at
    the head of the loop. The "stuff at end of switch to be reached only via
    'break'" was incrementing the instruction pointer, which wasn't wanted after
    branch instructions.

    -- chris
    Chris Uppal, Jul 11, 2006
    #16
  17. steve Guest

    On Mon, 10 Jul 2006 19:48:31 +0800, Luc The Perverse wrote
    (in article <>):

    > "Chris Uppal" <-THIS.org> wrote in message
    > news:44b23c3b$1$782$...
    >> Luc The Perverse wrote:
    >>
    >>> If it is, I would like to take a second to rant about all the damn times
    >>> I
    >>> have made worthless boolean variables to hold exit, fail, "error" or bad
    >>> values simply as an exit condition for a nested loop.

    >>
    >> Personally, I have never used the feature, and have only ever seen it used
    >> very
    >> rarely. Nor do I (ever) use boolean flags to control loop exit conditions
    >> and
    >> the like.
    >>
    >> IMO both should be avoided except in very special circumstances.
    >>
    >> -- chris
    >>
    >>
    >>

    >
    > You have never needed out of a nested loop?
    >
    > --
    > LTP
    >
    >>>

    >
    >


    there are those that believe that a loop is a "contract", and that if it
    loops for a given amount, that it should always complete that amount.

    Say as, opposed to being a loop for 20, but exiting at 10, personally i try
    to use a flag variable & "while"

    but there are those that use "try" catch to exit loops, and thrown
    exceptions.

    and vary rarely , like in 5 years of java , I will admit to 2 pieces of my
    code having exit labels
    but that was only to trap a fairly difficult validation of user data.

    So generally I consider i'm not completely insane.
    steve, Jul 11, 2006
    #17
    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. Replies:
    36
    Views:
    1,480
    Chris Uppal
    Jun 6, 2005
  2. Colin King

    Switch statement without break

    Colin King, Oct 9, 2005, in forum: C Programming
    Replies:
    7
    Views:
    454
    Skarmander
    Oct 10, 2005
  3. Alexander Korsunsky

    break inside of case- statement inside of loop

    Alexander Korsunsky, Feb 25, 2007, in forum: C Programming
    Replies:
    25
    Views:
    1,067
    Richard Heathfield
    Feb 27, 2007
  4. Cybex
    Replies:
    9
    Views:
    473
    Old Wolf
    Feb 12, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page