How to operate loops in Python with break and continue

Python break and Python continue allow you to terminate or interrupt a loop. These commands often work in conjunction with an “if” statement.

What are Python break and Python continue used for?

Python while loops and Python for loops are well known among internet programming languages. They allow users to automatically repeat clearly defined tasks within a predefined framework. Certain commands can also influence and refine the resulting loops. Python break and Python continue are good examples of this approach. These two statements interrupt while a loop is running and allow users to terminate a loop if something occurs. Therefore, in the event of an unwanted external influence, the loop will stop even if it has not reached the end.

What is Python break?

Break in Python terminates a loop completely when an external condition is given or not given. Python break is used within the code and is usually placed after an “if” statement.

How does Python break work?

Python break usually only comes into effect after one or more rounds, since it is inserted into the loop. Firstly, the loop starts and the stored condition for continuation or termination is checked. If the condition is false, the loop will be terminated at this point. If the condition is true, the loop will run through once completely and then start again with changed values. This is where the Python break comes in. If its defined condition is true, the loop continues. However, if the condition is false, the loop terminates at this point.

Python break example

The ordinary counting mechanism is a simple example of Python break. In this example, the “for” loop should count from 0 to 9. The condition is that the number is smaller than 10 in this loop. You probably already know this arrangement from our Python tutorial. A Python break which says that the loop should breakwhen the number 5 is reached can then be inserted. 5 is within the specified range, meaning that the loop will terminate and the code will continue after that. It will look like this:

for num in range(10):
    if num == 5:
		print("The termination condition is met")
		break
print(f"The current number is {num}")
print("Continuation after loop")

The output will result in this:

The current number is 0
The current number is 1
The current number is 2
The current number is 3
The current number is 4
The termination condition is fulfilled
Continuation after the loop

What is Python continue?

Python continue is similar to Python break in the sense that it also terminates the loop, but it resumes the loop as soon as a new value is issued. Therefore, only a part of the loop is skipped ifa certain termination condition is met. Python continue is also used inside the loop and is usually placed after an “if” statement. The statement is particularly useful if you want to exclude factors that occur more frequently, but you still want a loop to continue. Python continue makes the code more streamlined and avoids the various python problems which can occur.

How does Python continue work?

The general function is similar to Python break. The loop starts and the condition is queried for true or false.The procedure will normally repeat until the condition becomes false. However, Python continue has an intermediate query. The loop will continue normally if the answer satisfies this condition. However, if the Python continue condition is not met, the loop jumps to the beginning and runs with a new value.

Python continue example

Like the above example, a loop can also be created with Python continue. However, the loop should count, start at 0 and stop at 9 this time. The number is also smaller than 10 in this condition, but if the counting mechanism reaches 5, the loop should be interrupted, but not terminated. This is how the code is written:

for num in range(10):
    if num == 5:
        continue
print(f"The current number is {num}")
print("Continuation after loop")

The output results in:

The current number is 0
The current number is 1
The current number is 2
The current number is 3
The current number is 4
The current number is 6
The current number is 7
The current number is 8
The current number is 9
Continuation after the loop

The loop counts up to 4, interrupts at 5 and then continues counting as normal from 6 up to 10. The latter does not fulfil the full condition of the loop and is therefore terminated.

What is Python pass?

Python pass is another statement which can be used addition to Python break and Python continue. Python pass intervenes in the loop. However, it ensures that a certain condition is ignored. Below is a short pass statement which matches the example above:

for num in range(10):
    if num == 5:
        pass
print(f"The current number is {num}")
print("Continuation after loop")

This produces the following output:

The current number is 0
The current number is 1
The current number is 2
The current number is 3
The current number is 4
The current number is 5
The current number is 6
The current number is 7
The current number is 8
The current number is 9
Continuation after the loop

The program continues to function normally although the loop also gets to 5. You can find all the important information about Python pass in our Digital Guide where we delve into the uses and specifics of this statement.