python method of recording multiple nested loop out

Objective: case of multiple nested loops, the innermost layer when the cycle is aborted, the entire suspension directly multiple nested loops.

1 for i in range(10): # 循环2
2     for j in range(9): # 循环1
3         print(j)
4         if i ==3 and j == 2:
5             break  # break1
6     else:
7         continue
8     break  # break2

With emphasis on the use of break and continue

 

1 cycle mode is for-else

Results of cycle 1 only have two cases: 1. The cycle ends, the else part; 2. meet if the determination condition, the cycle interrupt, break under execution if.

So to say, if a complete cycle 1 cycle, then proceed to the next loop cycle 2, if the loop break the cycle 1, the loop 2 must also be interrupted, out of the entire nested loops (purpose)

In other words,

1. cycle 2, cycle break after break, must break the cycle 2, cycle 1 and therefore the same level, after cycle 1 put break2.

1 cycle of the case 1, the end of the cycle, the cycle time can not perform break2 2, and thus continue in a decentralized else, after the end of cycle 1 will continue execution portion else, continue this cycle is 2 acts, therefore skipped execution break2, circulation next cycle 2.

Thus, the objective was achieved.

 

Out of multiple nested loops there are many methods, such as use try ... except custom exception to jump out, but I like this, with the simple use of continue and break can be achieved, tips to solve big problems, hereby record .

Guess you like

Origin www.cnblogs.com/congyinew/p/11864923.html