break out of the multi-layer while loop flag

Sometimes when we write a while loop, there are multiple levels of nesting, and there are multiple while statements. If a certain condition break is met, because break can only jump out of the nearest while or for loop, but we don’t want to write many judgment conditions , So the validity and running speed of the code will be affected. So you can use the flag bit to jump out of the multi-layer loop.
Here is a small example, you can try to debug it:

#break跳出两层循环
i=0;j=0
Break1=0#利用标志位Break1跳出第二层while循环
while i<20:
    while j<10:
        if (i==5)and (j==8):
            Break1=1
            break
        else:
            j=j+1
    if Break1==1:
        break
    else:
        i=i+1
        j=0

Guess you like

Origin blog.csdn.net/qq_43511299/article/details/114965508