In Python, break and continue are loop control statements executed inside a loop. These statements either skip according to the conditions inside the loop or terminate the loop execution at some point. This tutorial explains break and continue statements in Python.



Break Statement

A break statement is used inside both the while and for loops. It terminates the loop immediately and transfers execution to the new statement after the loop. For example, have a look at the code and its output below:

Example:

count = 0

while count <= 100:
    print (count)
    count += 1
    if count == 3:
        break

Program Output:

0
1
2

In the above example loop, we want to print the values between 0 and 100, but there is a condition here that the loop will terminate when the variable count becomes equal to 3.

Continue Statement

The continue statement causes the loop to skip its current execution at some point and move on to the next iteration. Instead of terminating the loop like a break statement, it moves on to the subsequent execution.

Example:

for i in range(0, 5):
    if i == 3:
        continue
    print(i)

Program Output:

0
1
2
4

In the above example loop, we want to print the values between 0 and 5, but there is a condition that the loop execution skips when the variable count becomes equal to 3.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram