How to jump out of several nested control structures?

Discussion in 'Java' started by Shawn, Oct 20, 2006.

  1. Shawn

    Shawn Guest

    Hi,

    I am wondering how can I jump out of several layers of loops. break can
    only jump out of the inner most loop.

    for ( ...) //layer 1
    {
    for (...) //layer 2
    {
    for (...) //layer 3
    {
    //code
    if (jump==true) //How can I jump out of all the layers at once?

    }
    }

    }
     
    Shawn, Oct 20, 2006
    #1
    1. Advertising

  2. Patricia Shanahan, Oct 20, 2006
    #2
    1. Advertising

  3. On Fri, 20 Oct 2006 13:44:00 -0400, Shawn wrote:
    > I am wondering how can I jump out of several layers of loops. break
    > can only jump out of the inner most loop.


    No, it can jump much further than that. Label your loops and specify a
    label in the break statement:

    gurka:
    for (...) {
    for (...) {
    break gurka;
    }
    }

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 20, 2006
    #3
  4. Shawn

    Shawn Guest

    Patricia Shanahan wrote:

    >
    > No, break can jump out of any labeled statement enclosing it.
    >
    > See
    > http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6842
    >
    >
    > for the syntax and an example.
    >
    > Patricia


    Wow. break is similar to GOTO, according to the following statements
    from the link you gave to me:

    "A break statement with label Identifier attempts to transfer control to
    the enclosing labeled statement (§14.7) that has the same Identifier as
    its label; this statement, which is called the break target, then
    immediately completes normally. In this case, the break target need not
    be a while, do, for, or switch statement. A break statement must refer
    to a label within the immediately enclosing method or initializer block.
    There are no non-local jumps."

    I almost have never seen people using break as GOTO feature.
     
    Shawn, Oct 20, 2006
    #4
  5. Shawn wrote:
    > Patricia Shanahan wrote:
    >
    >>
    >> No, break can jump out of any labeled statement enclosing it.
    >>
    >> See
    >> http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6842
    >>
    >>
    >> for the syntax and an example.
    >>
    >> Patricia

    >
    > Wow. break is similar to GOTO, according to the following statements
    > from the link you gave to me:
    >
    > "A break statement with label Identifier attempts to transfer control to
    > the enclosing labeled statement (§14.7) that has the same Identifier as
    > its label; this statement, which is called the break target, then
    > immediately completes normally. In this case, the break target need not
    > be a while, do, for, or switch statement. A break statement must refer
    > to a label within the immediately enclosing method or initializer block.
    > There are no non-local jumps."
    >
    > I almost have never seen people using break as GOTO feature.


    It is very different from GOTO, because it can ONLY be used to break out
    of a statement enclosing the break.

    That said, I'm obviously aware of it, but I don't think I've used it so
    far in my own code.

    Patricia
     
    Patricia Shanahan, Oct 20, 2006
    #5
  6. Shawn

    Shawn Guest

    Patricia Shanahan wrote:


    >
    > It is very different from GOTO, because it can ONLY be used to break out
    > of a statement enclosing the break.


    "Break out of a statement enclosing the break". What does it mean? I am
    sorry.

    According to the cited paragraph, it doesnt' need for loop, while loop,
    etc. It seems the following code is legal:

    public class MyClass
    {
    ...//code

    public void myMethod()
    {
    ...//code
    if (wantToEnd) break here
    ...//more code


    here: System.out.println("I am going to exit");
    return;
    }

    }

    Do you think the above feature is GOTO?
     
    Shawn, Oct 20, 2006
    #6
  7. Shawn

    Shawn Guest

    Patricia Shanahan wrote:

    >
    > It is very different from GOTO, because it can ONLY be used to break out
    > of a statement enclosing the break.
    >
    > That said, I'm obviously aware of it, but I don't think I've used it so
    > far in my own code.
    >
    > Patricia


    Sorry. I see what you mean now.

    here:
    {
    ...//many layers inside
    if (jump==true) break here; //jump out of here scope
    ...//more code executed if not jumped

    }
    //reach here by jump or by normal execution
     
    Shawn, Oct 20, 2006
    #7
  8. Shawn

    Simon Brooke Guest

    in message <ehb1t0$8mi$>, Shawn ('')
    wrote:

    > I am wondering how can I jump out of several layers of loops. break can
    > only jump out of the inner most loop.


    Define your own exception class, wrap the whole lot in a try... catch, and
    throw your special exception when you want to jump out of the loop.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/

    ;; Conservatives are not necessarily stupid,
    ;; but most stupid people are conservatives -- J S Mill
     
    Simon Brooke, Oct 20, 2006
    #8
  9. Shawn

    Simon Brooke Guest

    in message <ehb6sj$a3j$>, Shawn ('')
    wrote:

    > Patricia Shanahan wrote:
    >
    >> No, break can jump out of any labeled statement enclosing it.
    >>
    >> See
    >>

    http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6842
    >>
    >>
    >> for the syntax and an example.

    >
    > Wow. break is similar to GOTO, according to the following statements
    > from the link you gave to me:


    Yup, that's why I prefer the 'special exception' solution. Jumping to
    labels is spaghetti programming in the making, whereas a special exception
    just unwinds the stack.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/

    my other car is #<Subr-Car: #5d480>
    ;; This joke is not funny in emacs.
     
    Simon Brooke, Oct 20, 2006
    #9
  10. Simon Brooke wrote:
    > Yup, that's why I prefer the 'special exception' solution. Jumping to
    > labels is spaghetti programming in the making, whereas a special exception
    > just unwinds the stack.


    IMHO using exceptions for this is a case of abuse. From my point of
    view refactoring to another method and using "return" is much cleaner -
    and probably faster, too. Before I'd use an exception for a non
    exceptional condition I'd rather use "break" with label. However, so
    far I never felt the need for any of those. I typically use the
    "return" solution or have proper loop conditions - whatever seems more
    appropriate.

    Regards

    robert
     
    Robert Klemme, Oct 20, 2006
    #10
  11. Shawn

    Mark Rafn Guest

    Shawn <> wrote:
    >Wow. break is similar to GOTO


    Nope. Break can only transfer control to an enclosing scope. It can only
    go to the end of a block. GOTO can jump anywhere. This is a humongous
    difference.

    That said, it can be abused. I've seen code that contained
    do {
    stuff;
    if (!something) break;
    more stuff;
    } while (false);

    As a way to avoid putting "more stuff" into a separate method because there
    were a lot of local variables that would have to be passed in and out.

    >There are no non-local jumps."


    This is the key phrase that makes break safe and goto unsafe.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Oct 21, 2006
    #11
  12. Shawn

    crazzybugger Guest

    Shawn wrote:
    > Hi,
    >
    > I am wondering how can I jump out of several layers of loops. break can
    > only jump out of the inner most loop.
    >
    > for ( ...) //layer 1
    > {
    > for (...) //layer 2
    > {
    > for (...) //layer 3
    > {
    > //code
    > if (jump==true) //How can I jump out of all the layers at once?
    >
    > }
    > }
    >
    > }


    Just use the return statement with no arguements...........suppose you
    have a loop say
    for(...){
    for(...){
    for(,.....){
    //your code
    if(yourConditionSatisfied)
    return
    }
    }
    }
    or you dont want the function to end
    do something like this
    try{
    for(...){
    for(...){
    for(...){
    //your code
    if(yourConditionSatisfied)
    throw new
    yourException();
    }
    }
    }
    }catch(YourException e){ //dummy }
    //continue your code here
     
    crazzybugger, Oct 21, 2006
    #12
  13. Shawn <> writes:

    > Wow. break is similar to GOTO,


    GOTO is a *general* purpose jump. Most languages need *special*
    purpose jumps - like Java's break, continue, while, if and for. The
    difference is that GOTO's jump-anywhere nature makes it hard to
    analyse flow compared to the restricted and block-oriented
    alternatives.

    The C# developers apparently chose to let the programmer shoot
    themselves in the foot like that. But only within a method or switch
    statement, of course.
     
    Tor Iver Wilhelmsen, Oct 21, 2006
    #13
  14. Mark Rafn wrote:
    >
    > That said, it can be abused. I've seen code that contained
    > do {
    > stuff;
    > if (!something) break;
    > more stuff;
    > } while (false);
    >
    > As a way to avoid putting "more stuff" into a separate method because there
    > were a lot of local variables that would have to be passed in and out.


    Code like this can often be written more sensibly. But if the programmer
    thought it not worth the costs of splitting into a separate method, the
    technique doesn't seem completely unreasonable. (For the record I almost
    did it once that I remember. I then just rearranged the code into a
    clearer form.)

    BTW, you don't need the do while false;:

    block: {
    stuff;
    if (!something) {
    break block; /***/
    }
    more stuff;
    }

    FWIW: I think all break statements should have labels (although that is
    unidiomatic) and continue should be outlawed (although I used it in an
    interview the other week). Oh, and nested loops tend to be an indicator
    that your method is getting a bit long (2D structures excepted).

    Tom Hawtin
     
    Thomas Hawtin, Oct 21, 2006
    #14
    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. Miguel Dias Moura
    Replies:
    3
    Views:
    4,402
    Bruno Sirianni
    Apr 3, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    754
    Emmanuel Delahaye
    Sep 24, 2005
  3. Peng Yu
    Replies:
    3
    Views:
    457
    alex23
    Sep 29, 2009
  4. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    199
    Max Lapshin
    Mar 16, 2007
  5. Eadwine Rose
    Replies:
    2
    Views:
    230
    Eadwine Rose
    Oct 15, 2006
Loading...

Share This Page