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.
A break statement is used inside both the
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:
count = 0 while count <= 100: print (count) count += 1 if count == 3: break
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.
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.
for i in range(0, 5): if i == 3: continue print(i)
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.