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
  • 相关阅读:
    版本回退
    时光机穿梭
    创建版本库
    安装Git
    Git简介
    Nexus私服安装
    eclipse中创建MAVEN-web项目
    AsyncTask的使用
    在子线程中更新UI,只能使用Handler
    使用VideoView播放视频
  • 原文地址:https://www.cnblogs.com/frankielf0921/p/5842235.html
Copyright © 2011-2022 走看看