How to break out deeply nested loops? (newbie)

Discussion in 'Ruby' started by Talha Oktay, Mar 30, 2006.

  1. Talha Oktay

    Talha Oktay Guest

    ------=_Part_7267_58209.1143742564044
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    I sometimes need to break out, next or redo deeply nested loops in differen=
    t
    levels instead of the innermost enclosing scope. Is there a facility to
    label blocks or loops and redo, next or break to the labeled blocks or
    loops as in some other programming languages?

    Thanks in advance.

    ------=_Part_7267_58209.1143742564044--
    Talha Oktay, Mar 30, 2006
    #1
    1. Advertising

  2. Talha Oktay

    Chris Alfeld Guest

    Not nicely.

    This is discussed in the Pickaxe. For example

    catch "BreakOuterLoop" do
    for i in 1..10
    print "out #{i}\n"
    for j in 1..10
    print "in #{j}\n"
    throw "BreakOuterLoop" if i+j > 16
    end
    end
    end

    On 3/30/06, Talha Oktay <> wrote:
    > I sometimes need to break out, next or redo deeply nested loops in differ=

    ent
    > levels instead of the innermost enclosing scope. Is there a facility to
    > label blocks or loops and redo, next or break to the labeled blocks or
    > loops as in some other programming languages?
    >
    > Thanks in advance.
    >
    >
    Chris Alfeld, Mar 30, 2006
    #2
    1. Advertising

  3. Talha Oktay

    James Britt Guest

    Talha Oktay wrote:
    > I sometimes need to break out, next or redo deeply nested loops in different
    > levels instead of the innermost enclosing scope. Is there a facility to
    > label blocks or loops and redo, next or break to the labeled blocks or
    > loops as in some other programming languages?
    >
    > Thanks in advance.
    >


    throw/catch

    http://www.ruby-doc.org/core/classes/Kernel.html#M001972


    --
    James Britt
    James Britt, Mar 30, 2006
    #3
  4. Talha Oktay schrieb:
    > I sometimes need to break out, next or redo deeply nested loops in different
    > levels instead of the innermost enclosing scope. Is there a facility to
    > label blocks or loops and redo, next or break to the labeled blocks or
    > loops as in some other programming languages?
    >
    > Thanks in advance.



    You may use throw, catch or continuations (for the fun of it) or just
    wrap some of your inner loops in their own method and use return.
    (i prefer the last version most of the time)

    cheers

    Simon
    Simon Kröger, Mar 30, 2006
    #4
    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. viza

    break or continue out of nested loops

    viza, Jul 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    997
    sunil
    Jul 17, 2003
  2. Edward C. Jones

    Pickle vs. eval for deeply nested objects

    Edward C. Jones, Feb 18, 2004, in forum: Python
    Replies:
    0
    Views:
    470
    Edward C. Jones
    Feb 18, 2004
  3. Ori Y
    Replies:
    1
    Views:
    396
    Bob Ippolito
    Feb 28, 2004
  4. Kirk Strauser
    Replies:
    1
    Views:
    284
    Kirk Strauser
    Jun 11, 2004
  5. Kay Schluehr
    Replies:
    7
    Views:
    323
    Kay Schluehr
    Jan 20, 2006
Loading...

Share This Page