看两个例子对比:
#例一:在while中的break
num_0 = 6
num_1 = 1
while num_1 <= 12:
if num_1 == num_0:
print("break the loop")
break
num_1 += 1
print("the num is now", num_1)
else:
print("else here")
'''
output:
the num is now 2
the num is now 3
the num is now 4
the num is now 5
the num is now 6
break the loop
'''
#例二: 当while执行是False,进入else
num_0 = 12
num_1 = 1
while num_1 < 12:
if num_1 == num_0:
print("break the loop")
break
num_1 += 1
print("the num is now", num_1)
else:
print("else here")
'''
output:
the num is now 2
the num is now 3
the num is now 4
the num is now 5
the num is now 6
the num is now 7
the num is now 8
the num is now 9
the num is now 10
the num is now 11
the num is now 12
else here
'''