break :跳出循环,我想知道跳出的循环范围是多少,是直接跳出所有循环,还是有限个数的循环
for i in range(3): # i取值为[0,5): 0,1,2,3,4 print("i = %d"%i) for j in range(3,9): if j > 5: print("j = %d"%j) else: break
输出:
i = 0 i = 1 i = 2
例2:
1 for i in range(3): # i取值为[0,5): 0,1,2,3,4 2 print("i = %d"%i) 3 for j in range(3,6): 4 if j < 5: 5 for k in range(6, 9): 6 if k > 7: 7 print("k = %d"%k) 8 else: break 9 else: 10 print("j = %d" % j)
输出: 可见break跳出的是5-8的一个for+if循环
第一次循环
i = 0
j = 5
第二次循环
i = 1
j = 5
第三次循环
i = 2
j = 5