• while语句


    while语句用于循环执行程序

    一、概念:
    条件循环是指:一个结构,导致一个程序要重复一定次数,当条件变为假,则循环结束。

    二、语法:

    1 while 条件:
    2      
    3     # 循环体
    4  
    5     # 如果条件为真,那么循环体则执行
    6     # 如果条件为假,那么循环体不执行
    基本循环

    执行语句可以是
    a、单个语句或语句块。
    b、判断条件是以任何表达式。
    c、任何非零、或非空(null)的值均为True。

    当判断条件为false时,循环结束:

    执行流程图:

    while循环表达式(s):
    1、通过条件判断,如果表达式为true。
    2、开始执行条件语句。
    3、开始continue,break的循环。
    4、如果条件判断为false,则退出循环体。
     1 #!/usr/bin/env python
     2 # -*- coding:utf8 -*-
     3 
     4 count = 0
     5 while (count < 9):
     6     print('The count is:'), count
     7     count = count + 1
     8 print("Good bye!")
     9 
    10 
    11 输出结果:
    12 The count is: 0
    13 The count is: 1
    14 The count is: 2
    15 The count is: 3
    16 The count is: 4
    17 The count is: 5
    18 The count is: 6
    19 The count is: 7
    20 The count is: 8
    21 Good bye!
    View Code

    break和continue的用法

    while 语句时还有另外两个重要的命令 continue,break 来跳过循环:
    continue 用于跳过该次循环
    break 则是用于退出循环
    此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:

     1 i = 1
     2 while i < 10:  
     3     i += 1
     4     if i%2 > 0:     # 非双数时跳过输出
     5         continue
     6     print (i)         # 输出双数2、4、6、8、10
     7  
     8 
     9 i = 1
    10 while 1:            # 循环条件为1必定成立
    11     print (i)         # 输出1~10
    12     i += 1
    13     if i > 10:     # 当i大于10时跳出循环
    14         break
    实例

    无限循环

    1 #!/usr/bin/python
    2 #coding=utf-8
    3 
    4 var = 1
    5 
    6 while var == 1 :  # 该条件永远为true,循环将无限执行下去
    7    num = raw_input("Enter a number  :") #表示需要界面输入值,用于交互print "You entered: ", num      #num是上面的变量,表示输人值在输出
    8 
    9 print ("Good bye!")
    View Code

    注意:以上的无限循环你可以使用 CTRL+C 来中断循环。(循环无止境,直到内存撑爆,会导致系统雪崩!)

    while循环中使用else语句

     while.....else表示这样的意思:

    1、while中的语句和普通的没有区别。
    2、else中的语句会在循环正常执行完(即while不是通过break跳出终端的)的情况下执行。
    3、当while 循环执行成功后,另外在执行附加条件else。
     1 #!/usr/bin/python
     2 
     3 count = 0
     4 while count < 5:
     5     print(count, "is less than 5")
     6     count = count + 1
     7 else :
     8     print(count,"is not less than 5")
     9 
    10 
    11 输出结果:
    12 0 is less than 5
    13 1 is less than 5
    14 2 is less than 5
    15 3 is less than 5
    16 4 is less than 5
    17 5 is not less than 5
    View Code

     简单语句组

    类似if语句的语法,如果你的while循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:

     1 #!/usr/bin/python 
     2 
     3 flag = 1
     4 while (flag): print 'Given flag is really true!' 
     5 
     6 
     7 输出结果:
     8 print "Good bye!"
     9 
    10 注意:以上的无限循环你可以使用 CTRL+C 来中断循环。
    View Code
  • 相关阅读:
    设计模式学习笔记——Bridge 桥接模式
    设计模式学习笔记——Adapter 适配器模式
    protoc protobuff安装
    docker-compose启动consul
    docker etcd 环境搭建
    nifi的去重方案设计(二)-外部存储mysql全局去重
    实现一套ES全文检索语法-到Lucene语法的转换工具,以实现在es外部兼容处理文本分词
    nifi的去重方案设计(一)-单队列内去重.md
    k8s 证书过期处理
    部分项目从kafka迁移至pulsar,近期使用中碰到了一些问题,勉强把大的坑踩完了,topic永驻,性能相关
  • 原文地址:https://www.cnblogs.com/abobo/p/8028452.html
Copyright © 2020-2023  润新知