Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
one time loop
#6
(Jul-25-2018, 09:14 PM)Skaperen Wrote: what will the break statements do without the loop?

Then you'll get a SyntaxError.
You are only allowed to use the continue/break statement inside loops.
In functions you can use also return, to break out of a loop.

Additionally you have the possibility to use else in for-loops. In this case the else block is only executed, if the loop finishes. Today I've written code, which uses this control flow.

def find_and_remove(sequence, **kwargs):
    for element in sequence:
        if all(element.get(key) == kwargs.get(key) for key in kwargs):
            result = element
            # break out of the loop
            # the else block is not executed in this case.
            break
    else:
        # if the loop finishes, the function returns explicit None
        return None
    sequence.remove(result)
    # the result is only returned, a match was found
    return result
Quote:the goal is to define where control ends up at when each if clause with a break reaches that break statement.

If I understood it right, you want to have some branches inside a loop, which decides to break out of this loop. Yes, this is possible and often used. In my example this is used.

Each block in Python has it's own scope (maybe there are corner cases).
When you use a continue/break statement in nested scopes (eg. nested loops and other scopes),
it bubbles up, until it reaches the loop.


def foo():
    for a in range(20):
        for b in range(10):
            for c in range(5):
                with something() as bar:
                    for line in bar:
                        if 'foo' in line:
                            break
                            # this statement will break the
                            # for line in bar: loop
If you want to break out of the loop for b in range(10) you can solve this also.
This example is not the final solution.

def foo():
    for a in range(20):
        for b in range(10):
            # we want to break out of this loop if a condition is true
            for c in range(5):
                with something(a, b, c) as bar:
                    for line in bar:
                        if 'foo' in line:
                            break
                            # this statement will break the
                            # for line in bar: loop
                        elif 'bar' in line:
                            # if this block is executed, we want to break out of the
                            # loop: for b in range(10)
I guess it's better to refactor this code a little bit. We can use functions to make it easier for us to control the flow and still have an overview what's going on.

def do_something(*args)
    with something(*args) as bar:
        for line in bar:
            if 'foo' in line:
                return True
            elif 'bar' in line:
                return False

def abc_range():
    result = True
    for a in range(20):
        for b in range(10):
            if not result:
                # this breaks out of the b-loop
                break
            for c in range(5):
                result = do_something(a, b, c)
                # now the result is set and we need to break
                # out of the c-loop, where the b-loop continues
                if not result:
                    break
I guess with more effort you can avoid deep nested loops with if-branches.

Quote:in other languages, i'd be using a branch instruction or a goto statement to do this.
Using goto in other languages, leads to unreadable code and often in unpredictable code.
In High-Level languages you'll not see any use of goto.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
one time loop - by Skaperen - Jul-25-2018, 03:39 PM
RE: one time loop - by Larz60+ - Jul-25-2018, 05:18 PM
RE: one time loop - by Skaperen - Jul-25-2018, 09:14 PM
RE: one time loop - by DeaD_EyE - Jul-26-2018, 01:29 AM
RE: one time loop - by Skaperen - Jul-26-2018, 03:45 AM
RE: one time loop - by Larz60+ - Jul-25-2018, 09:24 PM
RE: one time loop - by Skaperen - Jul-26-2018, 12:53 AM
RE: one time loop - by stranac - Jul-26-2018, 07:09 AM
RE: one time loop - by Skaperen - Jul-26-2018, 02:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to measure execution time of a multithread loop spacedog 2 2,929 Apr-24-2021, 07:52 AM
Last Post: spacedog
  Updating a matrix in a time interval inside a for loop vp1989 4 2,950 May-17-2020, 07:15 PM
Last Post: vp1989
  Loop independent of excecution time of a script Forelli 8 3,894 Feb-02-2020, 10:49 PM
Last Post: snippsat
  How to Display Multiple Time Tables With While Loop ZQ12 2 2,206 Nov-10-2019, 04:15 AM
Last Post: ZQ12
  Time execution of a "for loop" with zip different in 2 equivalent context sebastien 1 1,953 Oct-11-2019, 11:07 AM
Last Post: sebastien
  output list reducing each time through loop 3Pinter 6 3,589 Mar-19-2019, 01:31 PM
Last Post: perfringo
  Display 20 records at a time,data structure or loop pythonds 1 2,500 Mar-29-2018, 11:09 AM
Last Post: DeaD_EyE
  best pythonic one-time loop Skaperen 2 2,814 Feb-26-2018, 04:02 AM
Last Post: Skaperen
  For Loop, execute one time for every loop iteration dragan979 2 4,480 Feb-20-2018, 12:02 PM
Last Post: dragan979
  Code issue with time remaining loop. Python3 deboerdn2000 11 8,919 May-04-2017, 04:53 PM
Last Post: deboerdn2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020