參考鏈接: Python中的循環和控制語句(continue, break and pass)
1、break:跳出循環,不再執行?
?
?用在while和for循環中 用來終止循環語句,即循環條件沒有False條件或者序列還沒被完全遞歸完,也會停止執行循環語句 如果使用嵌套循環,break語句將停止執行最深層的循環,并開始執行下一行代碼?
?
?
2、continue:跳出本次循環,執行下一次?
?
?用在while和for循環中 用來跳過當前循環的剩余語句,然后繼續進行下一輪循環 循環到這個點的時候,執行continue這里的某些操作,執行完了之后,繼續執行滿足條件的這一層循環需要做的事情,不會終止這一層循環(只會跳出這一次).?
?continue 語句跳出本次循環,而break跳出整個循環。?
?
?
3、exit():結束/退出整個程序?
for element in "Python":??
? ? ?if element == "t":??
? ? ? ? ?exit()??
? ? ?else:
? ? ? ? ?print(element)?
#輸出:Py
?
4、pass:可以理解為“程序通過”,不做任何操作,只起到占位的作用?
for element in "Python":??
? ? if element == "y":??
? ? ? ? pass??
? ? else:??
? ? ? ? print(element)
#輸出:Pthon
?
學習整理至:python中break、continue 、exit() 、pass終止循環的區別