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.
The break statement stops the current loop immediately.
for variable in sequence:
if condition:
break
Once the break statement executes, Python exits the loop.
for i in range(1, 10):
if i == 5:
break
print(i)
When i becomes 5, the break statement stops the loop.
i = 1
while i <= 10:
if i == 6:
break
print(i)
i += 1
numbers = [10, 20, 30, 40, 50]
for number in numbers:
if number == 30:
break
print(number)
The loop stops when the value becomes 30.
text = "Python"
for letter in text:
if letter == "h":
break
print(letter)
while True:
number = int(input("Enter a number: "))
if number == 0:
break
print("You entered:", number)
Here, entering 0 stops the loop.
The break statement is commonly used to stop an intentionally infinite loop.
while True:
print("Running")
break
Although while True normally continues forever, the break statement stops it.
for i in range(1, 21):
if i > 10:
break
print(i)
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)
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
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.
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.
for i in range(1, 20):
if i % 2 == 0 and i > 10:
break
print(i)
When the loop reaches 12, the condition becomes True and the loop stops.
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")
The message Loop completed is not printed because the loop ended using break.
| 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: 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)
for i in range(1, 101):
if i == 6:
break
print(i)
Even though the range goes up to 100, the loop stops at 6.
values = (10, 20, 30, 40)
for value in values:
if value == 30:
break
print(value)
student = {
"name": "Rahul",
"age": 20,
"course": "Python"
}
for key in student:
if key == "age":
break
print(key)
The break statement must be used inside a for or while loop.
if True:
break
Question: What does the break statement do in Python?