循环语句 while

格式:

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
.....

 

转载于:https://www.cnblogs.com/wanglan/p/9542173.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值