Python break and Python continue allow you to terminate or interrupt a loop. These commands often work in con­junc­tion 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 pro­gram­ming languages. They allow users to auto­mat­ic­ally repeat clearly defined tasks within a pre­defined framework. Certain commands can also influence and refine the resulting loops. Python break and Python continue are good examples of this approach. These two state­ments 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 ter­min­ates a loop com­pletely 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 con­tinu­ation or ter­min­a­tion is checked. If the condition is false, the loop will be ter­min­ated at this point. If the condition is true, the loop will run through once com­pletely 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 ter­min­ates 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 ar­range­ment from our Python tutorial. A Python break which says that the loop should break when 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 ter­min­ates the loop, but it resumes the loop as soon as a new value is issued. Therefore, only a part of the loop is skipped if a certain ter­min­a­tion condition is met. Python continue is also used inside the loop and is usually placed after an `if´ statement. The statement is par­tic­u­larly useful if you want to exclude factors that occur more fre­quently, but you still want a loop to continue. Python continue makes the code more stream­lined 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 in­ter­me­di­ate 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 in­ter­rup­ted, but not ter­min­ated. 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, in­ter­rupts 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 ter­min­ated.

What is Python pass?

Python pass is another statement which can be used addition to Python break and Python continue. Python pass in­ter­venes 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 in­form­a­tion about Python pass in our Digital Guide where we delve into the uses and specifics of this statement.

Go to Main Menu