• 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。
    • 注意,无论何种情况,else的内容执行完后,随即退出整个while-else体系
    • else中可以用到while循环体中的数据,但在pycharm中使用时会飘黄
  • 相关阅读:
    vuejs中使用echart图表
    锚点链接
    如何动态修改网页的标题(title)?
    如何为图片添加热点链接?(map + area)
    cookie
    如何为你的网站添加标志性的图标(头像)呢?
    图片拖拽上传至服务器
    js定时器之setTimeout的使用
    input[type=file]中使用ajaxSubmit来图片上传
    input[type=file]样式更改以及图片上传预览
  • 原文地址:https://www.cnblogs.com/Guoxing-Z/p/11487705.html
Copyright © 2020-2023  润新知