成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術文章
文章詳情頁

語法 - Python中,break和continue的語句只能配合if用嗎?

瀏覽:104日期:2022-07-07 11:42:43

問題描述

Python中,break和continue的語句只能配合if用嗎?下圖來自廖雪峰網站

語法 - Python中,break和continue的語句只能配合if用嗎?

問題解答

回答1:

不是的,continue和break要在for,while等循環中使用,單獨的if語句中不能使用break和continue. 舉個例子

# Example 1for a in [1, 2, 3, 4]: if (a == 1):continue else:print(a)# 2# 3# 4

# Example 2for a in [1, 2, 3, 4]: print(a) continue

單獨的if不能使用continue和break

# Example 3if True: continue# SyntaxError: ’continue’ not properly in loop 回答2:

當然不是,break和continue用來跳出循環

標簽: Python 編程