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





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM