Python中,break和continue的語句只能配合if用嗎?
下圖來自廖雪峰網站
#
我想大声告诉你2017-05-18 11:00:06
不是的,continue和break要在for,while等迴圈中使用,單獨的if語句中不能使用break和continue.
舉個例子
# Example 1
for a in [1, 2, 3, 4]:
if (a == 1):
continue
else:
print(a)
# 2
# 3
# 4
# Example 2
for a in [1, 2, 3, 4]:
print(a)
continue
單獨的if不能使用continue和break
# Example 3
if True:
continue
# SyntaxError: 'continue' not properly in loop