格式:
while 条件: 循环体 无限循环 终止循环:1、改变条件,使其不成了
2、break 中断循环
死循环
while True: print("我们不一样")
循环100次
count = 1 flag = True while flag: print(count) count +=1 if count > 100: flag = False
或者也可以这样写:
count = 1
while count <= 100:
print(count)
count +=1
执行结果: . . . 95 96 97 98 99 100
0到100相加
count = 1 sum = 0 while count <=100: sum = sum + count count +=1 print(sum)
或
print(sum(range(101)))
break
while True: print(1111) print(2222) break print(333) 执行结果: 1111 2222
使用break循环100次
count = 1 while True: print(count) count +=1 if count > 100: break
continue:跳过本次循环
count = 1 while count <10: print(count) continue count +=1 执行结果: 1 1 1 1 1 1 1 1 .....