Python Break Statement

In Python, you can use the break statement to exit a loop prematurely when a specific condition is met. It is commonly used in while or for loops. Here’s how you can use break conditions in different scenarios:

1. Using break in a while loop:


i = 0
while True:
    print(i)
    i += 1
    if i == 5:  # Break condition
        break

Explanation: This loop runs indefinitely (while True) but breaks when i reaches 5.

2. Using break in a for loop:


for i in range(5):
    if i == 3:  # Break condition
        break
    print(i)

Explanation: This loop iterates through numbers from 0 to 4 but stops (breaks) when i equals 3.

3. Using break with an if condition:


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
    if num > 5:  # Break condition
        break
    print(num)

Explanation: The loop prints numbers until it encounters a number greater than 5, then exits the loop.

Python Break Statement – Interview Questions

Q 1: What is the break statement in Python?

Ans: It immediately exits the loop.

Q 2: Where can break be used?

Ans: Inside for and while loops.

Q 3: Does break stop only the current iteration?

Ans: No, it stops the entire loop.

Q 4: Can break be used in nested loops?

Ans: Yes, but it exits only the inner loop.

Q 5: Is break used with conditional statements?

Ans: Yes, it is usually placed inside an if condition.

Python Break Statement – Objective Questions (MCQs)

Q1. What is the purpose of the break statement in Python?






Q2. What will be the output of the following code?

for i in range(5):
if i == 3:
break
print(i)






Q3. The break statement can be used inside:






Q4. What happens when a break statement is executed inside nested loops?






Q5. What will be the output of the following code?

i = 1
while i <= 5:
if i == 4:
break
print(i)
i += 1






Related Python Break Statement Topics