Tristran Thorn
Tristran Thorn

Reputation: 89

How do I make 'continue' statement iterate the "mother" loop outside of loop where the statement is found? (Python)

So I have a code in this format:

for-loop1:
    for-loop2:
        if-statement:
            continue
        <code here>
        <code there>

The question is, how do I make the continue statement iterate for-loop1 and not just for-loop2? Putting the statement in line with the for-loop2 line would iterat for-loop1 without even executing the rest of the code first. Help :(

Upvotes: 0

Views: 139

Answers (1)

John Kugelman
John Kugelman

Reputation: 361997

If there's no code after for-loop2, a break will effectively restart the outer loop.

for-loop1:
    for-loop2:
        if-statement:
            break
        <code here>
        <code there>

If there is additional stuff after the inner loop then it'll take a bit more doing.

for-loop1:
    restart_loop1 = False

    for-loop2:
        if-statement:
            restart_loop1 = True
            break

        <code here>
        <code there>

    if restart_loop1:
        continue

    <more code here>

Upvotes: 1

Related Questions