"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Loop Control statements in Python : break, continue, pass

Loop Control statements in Python : break, continue, pass

Published on 2024-11-07
Browse:961

Loop Control statements in Python : break, continue, pass

In Python, we have 3 loop control statements : break, continue and pass.

break

When the condition satisfies, the loop breaks and comes out of the loop.

for i in range(10):
    print(i)
    if i == 5:
        break

# It will print : 0 to 5 and once the condition satisfies, 
# then the loop breaks.

continue

When the condition satisfies, it is skipped and moves on to next iteration in the loop.

for i in range(10):
    if i == 5:
        continue
    print(i)

# It will print : 0 to 9 except 5.

for i in range(10):
    if i != 5 :
        continue
    print(i)

# It will print just 5.

pass

It does nothing. It acts as a placeholder.

for i in range(10):
    if i == 5:
        pass
    print(i)

# It will not do anything, even if the condition satisfies
Release Statement This article is reproduced at: https://dev.to/debasmita-a/loop-control-statements-in-python-break-continue-pass-mmk?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3