Grammar - In Python, can the break and continue statements only be used with if?
我想大声告诉你
我想大声告诉你 2017-05-18 10:58:06
0
2
1605

In Python, can the break and continue statements only be used with if?
The picture below is from Liao Xuefeng’s website

我想大声告诉你
我想大声告诉你

reply all(2)
我想大声告诉你

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
    
某草草

Of course not, break and continue are used to break out of the loop

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!