zoukankan      html  css  js  c++  java
  • 循环退出

    /* for...else 语句,当for循环正常结束后,才会执行else语句。 */
    
    eg1:
    [root@localhost test1]# vim 11.py
    //ADD
    #!/usr/bin/python
    
    for i in xrange(10):
        print i
    else:
        print 'main end'
    
    [root@localhost test1]# python 11.py
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    main end
    [root@localhost test1]# vim 11.py
    //ADD
    #!/usr/bin/python
    
    for i in xrange(10):
        print i
        if i == 5:
            break
    else:
        print 'main end'
    
    /* 当 i遍历到5时,会退出循环。
      
      这样else就不会执行。   */
    
    [root@localhost test1]# python 11.py
    0
    1
    2
    3
    4
    5
    [root@localhost test1]# vim 11.py
    //ADD
    #!/usr/bin/python
    
    for i in xrange(10):
        if i == 3:
            continue
        if i == 5:
            break
        print i
    else:
        print 'main end'
    
    /* 这里的 “continue” ,让 i == 3 不执行,然后再继续执行下面的循环 */
    
    [root@localhost test1]# python 11.py
    0
    1
    2
    4
    [root@localhost test1]# vim 11.py
    //add
    #!/usr/bin/python
    
    for i in xrange(10):
        if i == 3:
            continue
        elif i == 5:
            continue
        elif i == 6:
            pass
        print i
    else:
        print 'main end'
    
    /* elif --加多几个条件
       pass --通过,不停止
    */
    
    [root@localhost test1]# python 11.py
    0
    1
    2
    4
    6
    7
    8
    9
    main end
  • 相关阅读:
    软工个人阅读作业3
    阅读作业2
    代码复审
    pair project elevator
    读《移山之道——VSTS软件开发指南》
    软件工程作业--词频统计
    个人阅读作业3
    个人阅读作业2
    代码复审
    结对项目电梯吐血总结
  • 原文地址:https://www.cnblogs.com/frankielf0921/p/5842235.html
Copyright © 2011-2022 走看看