首页  >  文章  >  后端开发  >  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