一.while循环总规则
1 while 条件: 2 循环体
二.无限循环
1 while 条件: 2 无限循环
1 while True: 2 print('我爱china')
三.循环输出1-100 改变条件跳出循环
1 count = 1 2 flag = True 3 # 标志位方便之后对变量的修改 4 while flag: 5 print(count) 6 count = count + 1 7 # count += 1 8 if count >= 101: 9 flag = False
1 count = 1 2 while count <= 100: 3 print(count) 4 count = count + 1 5 # count += 1
3.1输出1+2+3+4+...+100的和
1 count = 1 2 sum1 = 0 3 while count <= 100: 4 sum1 = sum1 + count 5 count = count + 1 6 print(sum1)
利用break实现
1 count = 1 2 sum1 = 0 3 while True: 4 sum1 += count 5 count += 1 6 if count > 100: 7 break 8 print(sum1)
当然实现方式一大堆更新中........................
3.2关键字break、continue
break:终止循环使其条件不成立
continue:结束本次循环,进行下一次循环
1 # break 2 print(11) 3 while True: 4 print(1111) 5 print(123) 6 break 7 print(78945) 8 print(12345678)
1 print(111) 2 count = 1 3 while count <= 20: 4 print(count) 5 continue 6 count += 1 7 # 输出全是1,相当于死循环了