循环语句:while\for\嵌套
循环控制语句:break\continue
break:跳出整个循环,不会再继续循环下去
continue:跳出本次循环,继续下一次循环
while循环:
1 count = 02 while (count < 9):3 print("count=",count)4 count += 15 print("while循环结束")
结果:
count= 0
count= 1count= 2count= 3count= 4count= 5count= 6count= 7count= 8while循环结束continue语句:
i = 1while i<10: i+=1 if i%2>0: #如果i%2的值大于0,则跳出本次循环,后面的语句不会执行,直接继续下一次循环, continue print(i)
结果:
2
46810break语句:
i = 1while 1: #死循环 print(i) i += 1 if i>10:#循环到i=11的时候,执行break,跳出整个while循环 break
结果:
1 2 3 4 5 6 7 8 9 10
else语句:
while-else 与 if-else一样
count = 0while count <5: print(count,"小于5") count = count + 1else: print(count,"大于等于5")
结果:
0 小于5
1 小于52 小于53 小于54 小于55 大于等于5