In Python, can the break and continue statements only be used with if?
The picture below is from Liao Xuefeng’s website
我想大声告诉你2017-05-18 11:00:06
No, continue and break must be used in for, while and other loops. Break and continue cannot be used in a separate if statement.
Give me an example
# 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
Continue and break cannot be used for separate if
# Example 3
if True:
continue
# SyntaxError: 'continue' not properly in loop