Lesson 23 of 70 – Python Break Statement
33%

Python Break Statement

The break statement is used to immediately stop a loop. When Python encounters break, the loop terminates and program execution continues with the statement after the loop.

Note: The break statement can be used with both for loops and while loops.
What is the Break Statement?

The break statement stops the current loop immediately.

for variable in sequence:

    if condition:
        break

Once the break statement executes, Python exits the loop.

Simple Break Example
for i in range(1, 10):

    if i == 5:
        break

    print(i)
Output:
1
2
3
4

When i becomes 5, the break statement stops the loop.

How Break Works
  1. The loop starts normally.
  2. Python checks the condition.
  3. When the break condition becomes True, break executes.
  4. The loop stops immediately.
  5. Python continues with the code after the loop.
Break with While Loop
i = 1

while i <= 10:

    if i == 6:
        break

    print(i)
    i += 1
Output:
1
2
3
4
5
Break with List
numbers = [10, 20, 30, 40, 50]

for number in numbers:

    if number == 30:
        break

    print(number)
Output:
10
20

The loop stops when the value becomes 30.

Break with String
text = "Python"

for letter in text:

    if letter == "h":
        break

    print(letter)
Output:
P
y
t
Break with User Input
while True:

    number = int(input("Enter a number: "))

    if number == 0:
        break

    print("You entered:", number)

Here, entering 0 stops the loop.

Break from Infinite Loop

The break statement is commonly used to stop an intentionally infinite loop.

while True:

    print("Running")

    break
Output:
Running

Although while True normally continues forever, the break statement stops it.

Break with Condition
for i in range(1, 21):

    if i > 10:
        break

    print(i)
Output:
1
2
3
4
5
6
7
8
9
10
Break in Nested Loop

When break is used inside a nested loop, it terminates the nearest loop in which it appears.

for i in range(1, 4):

    for j in range(1, 4):

        if j == 2:
            break

        print(i, j)
Output:
1 1
2 1
3 1
Break with Search

The break statement can be useful when searching for a particular value.

numbers = [10, 20, 30, 40, 50]

for number in numbers:

    if number == 30:
        print("Number found")
        break
Output:
Number found
Break in Login Program
while True:

    password = input("Enter password: ")

    if password == "1234":
        print("Login successful")
        break

    print("Wrong password")

The loop continues until the correct password is entered.

Break in Menu Program
while True:

    print("1. Start")
    print("2. Exit")

    choice = input("Enter choice: ")

    if choice == "2":
        break

    print("Program continues")

Selecting option 2 exits the loop.

Break with Even Numbers
for i in range(1, 20):

    if i % 2 == 0 and i > 10:
        break

    print(i)
Output:
1
2
3
4
5
6
7
8
9
10
11

When the loop reaches 12, the condition becomes True and the loop stops.

Break and Else

A loop's else block does not execute when the loop is terminated by a break statement.

for i in range(1, 6):

    if i == 3:
        break

    print(i)

else:
    print("Loop completed")
Output:
1
2

The message Loop completed is not printed because the loop ended using break.

Break vs Continue
Break Continue
Stops the entire loop. Skips the current iteration.
Execution continues after the loop. Loop continues with the next iteration.
Used when we want to terminate a loop. Used when we want to skip certain values.
Break vs Pass

break: Stops the loop.

for i in range(5):

    if i == 2:
        break

    print(i)

pass: Does nothing and allows execution to continue.

for i in range(5):

    if i == 2:
        pass

    print(i)
Break with Range
for i in range(1, 101):

    if i == 6:
        break

    print(i)
Output:
1
2
3
4
5

Even though the range goes up to 100, the loop stops at 6.

Break with Tuple
values = (10, 20, 30, 40)

for value in values:

    if value == 30:
        break

    print(value)
Output:
10
20
Break with Dictionary
student = {
    "name": "Rahul",
    "age": 20,
    "course": "Python"
}

for key in student:

    if key == "age":
        break

    print(key)
Output:
name
Important: Break Only Works Inside a Loop

The break statement must be used inside a for or while loop.

if True:
    break
This will cause an error because break is not inside a loop.
Key Points
  • The break statement stops a loop immediately.
  • It can be used with for loops and while loops.
  • After break, Python continues with the code after the loop.
  • Break is useful for stopping a loop when a condition is met.
  • In nested loops, break stops the nearest loop.
  • A loop's else block does not execute when the loop ends through break.
  • Break is different from continue.
  • Break must be used inside a loop.

🧠 Quick Quiz

Question: What does the break statement do in Python?