while循环中break、continue的应用

循环语句

while循环

  • while True : (无限循环)

break : 跳出整个循环

continue : 跳过这次循环,进行下次循环(伪装成循环体中最后一行)

  • # print(1111)
    # while True:   # 死循环
    #     print("坚强")
    #     print("过火")
    #     print("鸡你太美")
    #     print("大碗宽面")
    # print(333)  --输出结果 1111  坚强 过火 鸡你太美 大碗宽面 坚强···*4 不会输出333
    
    # count = 0
    # while True:  # 死循环
    #     count = count + 1  # 先执行 = 右边的内容
    #     if count == 5:
    #         print(count)
    #         break # 有限循环    --输出结果 5 循环4次(从0-3)
    
    # 输出1,3,5,7,9
    
    # count = 1
    # while count < 10:
    #     print(count)
    #     count = count + 2
    
    # a = 0
    # while a < 10:
    #     a += 1
    #     if a % 2 == 0:
    #         continue
    #     else:
    #         print(a)
    
    # a=0
    # while a<3:
    #     a += 1
    #     print (a)
    #     continue
    # else:          #此处else意为平级的while a>=3:   !!!
    #     print(111)  ---输出结果为:1 2 3 111
    
    # print(222)
    # count = 0
    # while count < 3:
    #     print(count)
    #     count = count + 1
    # print(111)  ---输出结果为:222 0 1 2 111
    
    # print(222)
    # count = 0
    # while count < 3:
    #     print(count)
    #     count = count + 1
    #     break
    # print(111)  ---运行结果 : 222 0 111
    
    # print(222)
    # count = 0
    # while count < 3:
    #     print(count)
    #     count = count + 1
    #     break
    # else:
    #     print(111)  ---运行结果: 222 0   while-else是一个整体,break跳出是跳出整个循环

while else 体系

  • 当while的条件满足或为True时,走while的结果,当while的条件不再满足以后,走else的结果;当while条件不满足或为False时,执行else。注意:while跟else的关系如下图,且每次自己的循环体部分走完后都要回最开始的部分重新判断去向。

猜你喜欢

转载自www.cnblogs.com/Guoxing-Z/p/11487705.html
今日推荐