python break and continue

for m in range(3):
    if m == 0:
        for n in  range(3):
            if n == 1:
                print(m,n)
                continue
                print(n)
            if n == 1:
                print(m,n)
       if n == 2:
         print(m,n)
if m == 1: for x in range(3): if x == 1: print(m,x) break print(n) if x == 2: print(m,x) else: print(m,n,x) D:\untitled\venv\Scripts\python.exe D:/untitled/bogls/jia2.py 0 1
0 2 1 1 1 2 1 Process finished with exit code 0

To understand the interruption in python, let's first look at two sentences, break: jump out of the entire for loop, continue: jump out of this loop (continue is rarely used). Python code is executed from top to bottom, and the loop will be executed back and forth until the loop ends or is interrupted.

Then analyze the above code

When 'm==0', enter the 'for n in range(3)' loop, when 'n==0', the three conditions in the if statement in the loop are not satisfied, and the loop continues with 'n==1' If the condition is met, execute the if statement to output '0, 1', and then execute continue to jump out of this loop (the next output statement 'print(n)' is not executed and the current loop of 'n==1' is jumped out, and the following will not be judged two if statements), enter the 'n==2' loop, judge three if statements, and the third output (m, n).

Enter the 'for x in range(3)' loop when 'm==1', satisfy the first if statement when 'x==1', output (m, n), then execute break, and jump out of the entire 'for' x in range(3)' loop. Now there is only one total loop executing 'for m in range(3)', and the total loop finishes executing the else statement.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325163150&siteId=291194637