Python中的break跟continue的用法

Python中的break和continue的用法

一、continue的用法(结束当前的循序,进行下一个数的循环)

# *****************************************************************************
# This is a program to illustrate the useage of continue in Python.
# If you want to stop executing the current iteration of the loop and skip ahead to the next 
# continue statement is what you need. 
# ****************************************************************************
for i in range (1,6):
    print
    print 'i=',i,
    print 'Hello,how',
    if i==3:
        continue
    print 'are you today?'

运行结果如下:

>>> ================================ RESTART ================================
>>>

i= 1 Hello,how are you today?

i= 2 Hello,how are you today?

i= 3 Hello,how
i= 4 Hello,how are you today?

i= 5 Hello,how are you today?
>>> 


二、break的用法(结束总的循环)

# *****************************************************************************
# This is a program to illustrate the useage of break in Python.
# What if we want to jump out of the loop completely—never finish counting, or give up
# waiting for the end condition? break statement does that.
# ****************************************************************************
for i in range (1,6):
    print
    print 'i=',i,
    print 'Hello,what is ',
    if i==3:
        break
    print 'the weather today?'
运行结果如下:

>>> ================================ RESTART ================================
>>>

i= 1 Hello,what is  the weather today?

i= 2 Hello,what is  the weather today?

i= 3 Hello,what is
>>>