Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break from inner loop to the beginning of the outer loop

Tags:

python

What's the best way to break from inner loop so I reach beginning of the outer loop

while condition:
    while second_condition:
        if some_condition_here:
            get_to_the_beginning_of_first_loop

Right now I got something like

while condition:
    while second_condition:
        if condition1:
            break
    if condition1:
        continue
like image 703
P. Bolfa Avatar asked Mar 07 '23 18:03

P. Bolfa


1 Answers

Python has the option of an else: clause for while loops. This is called if you do not call break, so these are equivalent:

while condition:
    while second_condition:
        if condition1:
            break
    if condition1:
        continue
    do_something_if_no_break()

and:

while condition:
    while second_condition:
        if condition1:
            break
    else:
        do_something_if_no_break()
like image 170
Arthur Tacca Avatar answered Apr 08 '23 13:04

Arthur Tacca