循环退出

/* 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

 

posted @ 2016-09-05 14:47  Frankiee  阅读(319)  评论(0编辑  收藏  举报