# python 如何在多层循环中使用break/continue

【阅读全文】

print('=============单层循环使用=============')

for n in range(10):

if n == 9:

print('退出当前循环，当前n = ', n)

break

elif n == 4:

print('跳过当前循环，当前n = ', n)

continue

print('当前n = ', n)


# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9


print('=============双层循环使用=============')

for m in range(10):

for n in range(10):

if n == 9:

print('退出当前循环，当前n = ', n)

break

elif n == 4:

print('跳过当前循环，当前n = ', n)

continue

print('当前n = ', n)

if m == 8:

break


# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9

# 当前n =  0
# 当前n =  1
# 当前n =  2
# 当前n =  3
# 跳过当前循环，当前n =  4
# 当前n =  5
# 当前n =  6
# 当前n =  7
# 当前n =  8
# 退出当前循环，当前n =  9


print('=============三层循环使用=============')

for i in range(3):

for j in range(3):

for k in range(3):

if k in [1, 2, 3]:

continue

print('当前k = ', k)

if j in [1, 2, 3]:

continue

print('当前j = ', j)

if i in [2, 3]:

break

print('当前i = ', i)


# =============三层循环使用=============
# 当前k =  0
# 当前j =  0
# 当前k =  0
# 当前k =  0
# 当前i =  0
# 当前k =  0
# 当前j =  0
# 当前k =  0
# 当前k =  0
# 当前i =  1
# 当前k =  0
# 当前j =  0
# 当前k =  0
# 当前k =  0


【往期精彩】

python实现excel数据与mysql数据库互通有无！

python 实现超快窗口截图，自动获取当前活动窗口并展示截图...

posted @ 2022-05-22 20:09  Python集中营  阅读(62)  评论(0编辑  收藏  举报