multiple breaks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TP

    multiple breaks

    Hi everybody,

    Several means to escape a nested loop are given here:

    Given the following code (that doesn't work): while True: # Snip: print out current state while True: ok = get_input("Is this ok? (y/n)") if ok.lower() == "y&...


    According to this page, the best way is to modify the loop by affecting the
    variables that are tested in the loops. Otherwise, use exception:

    "If, for some reason, the terminating conditions can't be worked out,
    exceptions are a fall-back plan."

    In the following example, is this possible to affect the two iterators to
    escape the two loops once one "j" has been printed:

    for i in range(5):
    for j in range(i):
    print j
    # I would type "break 2" in shell bash
    # In C, I would set j=i-1 and i=4
    # In Python, is this possible to affect the two iterators?

    Or the only means is to use exception?

    Thanks in advance

    Julien

    --
    python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+ ,\'Z
    (55l4('])"

    "When a distinguished but elderly scientist states that something is
    possible, he is almost certainly right. When he states that something is
    impossible, he is very probably wrong." (first law of AC Clarke)
  • Chris Rebert

    #2
    Re: multiple breaks

    On Thu, Nov 13, 2008 at 2:07 AM, TP <Tribulations@p aralleles.inval idwrote:
    Hi everybody,
    >
    Several means to escape a nested loop are given here:
    >
    Given the following code (that doesn't work): while True: # Snip: print out current state while True: ok = get_input(&quot;Is this ok? (y/n)&quot;) if ok.lower() == &quot;y&...

    >
    According to this page, the best way is to modify the loop by affecting the
    variables that are tested in the loops. Otherwise, use exception:
    >
    "If, for some reason, the terminating conditions can't be worked out,
    exceptions are a fall-back plan."
    >
    In the following example, is this possible to affect the two iterators to
    escape the two loops once one "j" has been printed:
    >
    Non-exception alternative:

    done = False
    for i in range(5):
    for j in range(i):
    print j
    done = True
    break
    # I would type "break 2" in shell bash
    # In C, I would set j=i-1 and i=4
    # In Python, is this possible to affect the two iterators?
    if done:
    break
    >
    Or the only means is to use exception?
    No, you could add a boolean variable and a break condition like above.

    Cheers,
    Chris
    --
    Follow the path of the Iguana...

    >
    Thanks in advance
    >
    Julien
    >
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+ ,\'Z
    (55l4('])"
    >
    "When a distinguished but elderly scientist states that something is
    possible, he is almost certainly right. When he states that something is
    impossible, he is very probably wrong." (first law of AC Clarke)
    --

    >

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: multiple breaks

      On Thu, 13 Nov 2008 11:07:25 +0100, TP wrote:
      According to this page, the best way is to modify the loop by affecting
      the variables that are tested in the loops. Otherwise, use exception:
      >
      "If, for some reason, the terminating conditions can't be worked out,
      exceptions are a fall-back plan."
      >
      In the following example, is this possible to affect the two iterators
      to escape the two loops once one "j" has been printed:
      >
      for i in range(5):
      for j in range(i):
      print j
      # I would type "break 2" in shell bash # In C, I would set j=i-1
      and i=4
      # In Python, is this possible to affect the two iterators?
      >
      Or the only means is to use exception?
      You could put the code into its own, maybe local, function and use
      ``return``.

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Peter Otten

        #4
        Re: multiple breaks

        TP wrote:
        Hi everybody,
        >
        Several means to escape a nested loop are given here:
        >
        >
        http://stackoverflow.com/questions/1...oops-in-python
        >
        According to this page, the best way is to modify the loop by affecting
        the variables that are tested in the loops. Otherwise, use exception:
        >
        "If, for some reason, the terminating conditions can't be worked out,
        exceptions are a fall-back plan."
        >
        In the following example, is this possible to affect the two iterators to
        escape the two loops once one "j" has been printed:
        >
        for i in range(5):
        for j in range(i):
        print j
        # I would type "break 2" in shell bash
        # In C, I would set j=i-1 and i=4
        # In Python, is this possible to affect the two iterators?
        >
        Or the only means is to use exception?
        Here's one way to turn multiple iterators into a single one:

        def pairs(n):
        for i in range(n):
        for k in range(i):
        yield i, k

        for i, k in pairs(5):
        print i, k

        I've yet to see an example where a multi-level break would improve the
        code's readability. It is typically a speed hack.

        Peter

        PS: Have a look into the itertools if you consider that approach.

        Comment

        • Steve Holden

          #5
          Re: multiple breaks

          Chris Rebert wrote:
          On Thu, Nov 13, 2008 at 2:07 AM, TP <Tribulations@p aralleles.inval idwrote:
          >Hi everybody,
          >>
          >Several means to escape a nested loop are given here:
          >>
          >http://stackoverflow.com/questions/1...oops-in-python
          >>
          >According to this page, the best way is to modify the loop by affecting the
          >variables that are tested in the loops. Otherwise, use exception:
          >>
          >"If, for some reason, the terminating conditions can't be worked out,
          >exceptions are a fall-back plan."
          >>
          >In the following example, is this possible to affect the two iterators to
          >escape the two loops once one "j" has been printed:
          >>
          >
          Non-exception alternative:
          >
          done = False
          >for i in range(5):
          > for j in range(i):
          > print j
          done = True
          break
          > # I would type "break 2" in shell bash
          > # In C, I would set j=i-1 and i=4
          > # In Python, is this possible to affect the two iterators?
          if done:
          break
          >Or the only means is to use exception?
          >
          No, you could add a boolean variable and a break condition like above.
          >
          Though I would have to ask why you would want to. An exception seems
          rather cleaner, though of course tastes vary.

          regards
          Steve
          --
          Steve Holden +1 571 484 6266 +1 800 494 3119
          Holden Web LLC http://www.holdenweb.com/

          Comment

          • Robert Lehmann

            #6
            Re: multiple breaks

            On Thu, 13 Nov 2008 02:16:32 -0800, Chris Rebert wrote:
            On Thu, Nov 13, 2008 at 2:07 AM, TP <Tribulations@p aralleles.inval id>
            wrote:
            >>
            >In the following example, is this possible to affect the two iterators
            >to escape the two loops once one "j" has been printed:
            >>
            Non-exception alternative:
            >
            done = False
            for i in range(5):
            for j in range(i):
            print j
            done = True
            break
            if done:
            break
            Another alternative is explicitly jumping in the outer loop::

            for i in range(5): # use xrange for larger ranges
            for j in range(i):
            print j
            break
            else:
            continue # applies already to the outer loop
            break

            HTH,

            --
            Robert "Stargaming " Lehmann

            Comment

            Working...