首頁  >  文章  >  後端開發  >  Python 中的循環控制語句:break、continue、pass

Python 中的循環控制語句:break、continue、pass

WBOY
WBOY原創
2024-08-23 06:01:36949瀏覽

Loop Control statements in Python : break, continue, pass

在Python中,我們有3種循環控制語句:break、continue和pass。

休息

當條件滿足時,循環中斷並跳出迴圈。

for i in range(10):
    print(i)
    if i == 5:
        break

# It will print : 0 to 5 and once the condition satisfies, 
# then the loop breaks.

繼續

當條件滿足時,它會被跳過並進入循環中的下一個迭代。

for i in range(10):
    if i == 5:
        continue
    print(i)

# It will print : 0 to 9 except 5.

for i in range(10):
    if i != 5 :
        continue
    print(i)

# It will print just 5.

經過

它什麼也沒做。它充當佔位符。

for i in range(10):
    if i == 5:
        pass
    print(i)

# It will not do anything, even if the condition satisfies

以上是Python 中的循環控制語句:break、continue、pass的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn