Python-循环-while

'''
# 打印10遍'Hello world'
i = 0
while i < 10:
    print('Hello world!')
    i += 1


# 打印1~100直接所有的整数
# 计算1 + 2 + ... + 100的和
i = 1
s = 0
while i <= 100:
    # print(i)
    s += i
    i += 1
print('1+2+...+100 = {}'.format(s))


# 打印1~100之间能够被3整除的数
i = 1
while i <= 100:
    if i % 3 == 0:
        print(i)
    i += 1

# 打印10遍'Hello world!'
i = 0
while True:
    print('Hello world!')
    i += 1
    if i >= 10:
        # 跳出循环
        break

i = 1
while i <= 100:
    if i % 3 == 0:
        i += 1
        # 结束本次循环,进入下次循环
        continue
    print(i)
    i += 1
'''

i = 1
while i <= 10:
    print(i)
    i += 1
    break
else:
    # 语句块:循环正常结束会执行,异常(break)退出不执行
    print('循环正常退出')

猜你喜欢

转载自blog.csdn.net/huaxiawudi/article/details/81165354
今日推荐