Home Tutorials How to Break for Loop in Python

How to Break for Loop in Python

0

Python break for loop examples is a great way to learn how to use the Python break statement. A Python break statement allows you to exit a loop prematurely. In this blog post, we will provide Python break-for-loop examples so that you can become a Python break master!

How to break for Loop

The Python break statement is a great tool to use when you need to exit a loop prematurely.

Here is the first Python break example:

i = 0
while i <= 100:
	if i == 50:
		break
	print(i)
	i += 1

In the example above, we use the Python break statement to exit the loop when the value of `i` is equal to `50`. The output of this Python code would be:

“`

0

49

“`

Read more: 10 Best Ways To Use While Loop In Python

As you can see, the break statement allowed us to exit the loop when `i` was equal to `50`. Let’s take a look at another Python break example:

In this second break example, we use the break statement to exit the loop when the value of `i` is equal to `75`. The output of this Python code would be:

“`

0

74

“`

As you can see, the break statement allowed us to exit the loop when `i` was equal to `75`. Let’s take a look at one more Python break example:

[/php]

#python

i = 0
while i <= 100:
if i % 25 == 0:
break
print(i)
i += 15

[/php]

In this break for loop example, we use the break statement to exit the loop when the value of `i` is divisible by `25`. The output of this Python code would be:

0

15

30

45

60

75

As you can see, the break statement allowed us to exit the loop when `i` was divisible by `25`.

Read More: 10 Ways to Use Python For Loop, What is For Loop in Python?

LEAVE A REPLY

Please enter your comment!
Please enter your name here