1 count = 0 2 while count <= 5 : 3 count += 1 4 if count == 3:pass 5 print("Loop",count) 6 7 else: 8 print("循环执行完啦") 9 print("-----out of while loop ------")
结果:
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环执行完啦
—–out of while loop ——
1 count = 0 2 while count <= 5 : 3 count += 1 4 if count == 3:break 5 print("Loop",count) 6 7 else: 8 print("循环执行完啦") 9 print("-----out of while loop ------")
Loop 1
Loop 2
—–out of while loop ——
结论:while循环正常执行完不会执行else里边的代码,如果while循环被break中断则会执行else里边的代码