break/continue - newbe

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

    #1

    break/continue - newbe

    I have trouble sometimes figuring out where
    break and continue go to. Is there some easy
    way to figure it out, or a tool?
    TIA
    Ann


  • Jeff Shannon

    #2
    Re: break/continue - newbe

    Ann wrote:
    [color=blue]
    >I have trouble sometimes figuring out where
    >break and continue go to. Is there some easy
    >way to figure it out, or a tool?
    >
    >[/color]

    Break and continue always operate on the most-nested loop that's
    currently executing. To show an example, let's add some line numbers to
    some code...

    1) while spam:
    2) if foo(spam):
    3) continue
    4) for n in range(spam):
    5) if bar(n):
    6) break
    7) results.append( baz(spam, n))
    8) spam = spam - 1

    Now, when this loop runs, when foo(spam) evaluates as True we execute
    the continue at line 3. At this time, we're running code that's at the
    loop-nesting level just inside 'while spam' (line 1), so line 1 is the
    loop statement that's affected by the continue on line 3. If line 3 is
    triggered, then we skip lines 4-8 and go back to line 1.

    If line 3 is *not* triggered, then we enter a for loop at line 4.
    There's a break at line 6; if this gets triggered, then we look
    backwards to find the most-current (i.e. most nested) loop, which is now
    that for loop on line 4. So we break out of that for loop (which
    comprises lines 4-7), and drop down to line 8.

    So, in general, the way to determine how break and continue will affect
    program flow is to look backwards (up) for the most recent loop
    statement; the break/continue will be inside that statement's dependent
    body. Break will drop you down to the next line after the loop body,
    and continue will bring you back up to the top of the loop body and the
    start of the next loop iteration.

    Jeff Shannon
    Technician/Programmer
    Credit International

    Comment

    • Ann

      #3
      Re: break/continue - newbe


      "Jeff Shannon" <jeff@ccvcorp.c om> wrote in message
      news:10sh1ct2oj 519da@corp.supe rnews.com...[color=blue]
      > Ann wrote:
      >[color=green]
      > >I have trouble sometimes figuring out where
      > >break and continue go to. Is there some easy
      > >way to figure it out, or a tool?
      > >
      > >[/color]
      >
      > Break and continue always operate on the most-nested loop that's
      > currently executing. To show an example, let's add some line numbers to
      > some code...
      >
      > 1) while spam:
      > 2) if foo(spam):
      > 3) continue
      > 4) for n in range(spam):
      > 5) if bar(n):
      > 6) break
      > 7) results.append( baz(spam, n))
      > 8) spam = spam - 1
      >
      > Now, when this loop runs, when foo(spam) evaluates as True we execute
      > the continue at line 3. At this time, we're running code that's at the
      > loop-nesting level just inside 'while spam' (line 1), so line 1 is the
      > loop statement that's affected by the continue on line 3. If line 3 is
      > triggered, then we skip lines 4-8 and go back to line 1.
      >
      > If line 3 is *not* triggered, then we enter a for loop at line 4.
      > There's a break at line 6; if this gets triggered, then we look
      > backwards to find the most-current (i.e. most nested) loop, which is now
      > that for loop on line 4. So we break out of that for loop (which
      > comprises lines 4-7), and drop down to line 8.
      >
      > So, in general, the way to determine how break and continue will affect
      > program flow is to look backwards (up) for the most recent loop
      > statement; the break/continue will be inside that statement's dependent
      > body. Break will drop you down to the next line after the loop body,
      > and continue will bring you back up to the top of the loop body and the
      > start of the next loop iteration.
      >
      > Jeff Shannon
      > Technician/Programmer
      > Credit International
      >[/color]
      Thanks Jeff, that solves my problem. BTW: is there an easy way to
      break/continue out more than one level?


      Comment

      • Fredrik Lundh

        #4
        Re: break/continue - newbe

        "Ann" <Ann@nospam.inv alid> wrote:
        [color=blue]
        > Thanks Jeff, that solves my problem. BTW: is there an easy way to
        > break/continue out more than one level?[/color]

        not really; to continue more than one level, you have to break out of the
        inner loop, and make sure the logic in that loop body brings you back to
        the top.

        to break more than one level, you can:

        1) use an exception

        class LeaveLoop(Excep tion): pass

        def myfunction():
        try:
        for a in ...:
        for b in ...:
        if c:
        raise LeaveLoop # break out of both loops
        do something
        except LeaveLoop:
        pass

        2) organize your code so you can use return rather than "multi-break"

        def myfunction():
        calculate()

        def calculate():
        for a in ...:
        for b in ...:
        if c:
        return
        do something

        3) use flags

        def myfunction():
        run = 1
        for a in ...:
        for b in ...:
        if c:
        run = 0
        if not run:
        break
        if not run:
        break

        Solution 2 is almost always the best way to do this; solution 3 is often the worst.

        </F>



        Comment

        • John Machin

          #5
          Re: break/continue - newbe


          Ann wrote:[color=blue]
          > I have trouble sometimes figuring out where
          > break and continue go to. Is there some easy
          > way to figure it out, or a tool?
          > TIA
          > Ann[/color]

          You need a tool called py2ftn. It would convert statements like "if a >
          b: break" to e.g. "IF(A.GT.B)GOTO 12345".

          A longer example; this:

          while 1:
          blah1
          if cond1: break
          blah2
          if cond2: continue
          blah3

          becomes:

          10000 IF(1)10001,1000 3,10001
          10001 CONTINUE
          BLAH1
          IF(COND1)GOTO10 003
          BLAH2
          IF(COND2)GOTO10 002
          BLAH3
          10002 CONTINUE
          GOTO10000
          10003 CONTINUE

          HTH,
          John

          Comment

          • John Machin

            #6
            Re: break/continue - newbe


            Ann wrote:[color=blue]
            > I have trouble sometimes figuring out where
            > break and continue go to. Is there some easy
            > way to figure it out, or a tool?
            > TIA
            > Ann[/color]

            You need a tool called py2ftn. It would convert a sequence of
            statements like:

            while 1:
            blah1
            if cond1: break
            blah2
            if cond2: continue
            blah3

            to:

            10000 IF(1)10001,1000 3,10001
            10001 CONTINUE
            BLAH1
            IF(COND1)GOTO10 003
            BLAH2
            IF(COND2)GOTO10 002
            BLAH3
            10002 CONTINUE
            GOTO10000
            10003 CONTINUE

            HTH,
            John

            Comment

            Working...