Home > Backend Development > Python Tutorial > Loop Control statements in Python : break, continue, pass

Loop Control statements in Python : break, continue, pass

WBOY
Release: 2024-08-23 06:01:36
Original
1269 people have browsed it

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.
Copy after login

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.

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

# It will print just 5.
Copy after login

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
Copy after login

The above is the detailed content of Loop Control statements in Python : break, continue, pass. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template