Loop Control statements in Python : break, continue, pass
Debasmita Adhikari

Debasmita Adhikari @debasmita-a

About: Hi, I am Debasmita. I am a software engineer with 9+ years of experience in Web application testing. You will be seeing me writing mostly about automation testing.

Joined:
May 21, 2024

Loop Control statements in Python : break, continue, pass

Publish Date: Aug 22 '24
0 0

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.
Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode
for i in range(10):
    if i != 5 :
        continue
    print(i)

# It will print just 5.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment