0% completed
The break
statement in Python is used to exit a loop immediately when a certain condition is met. Instead of running until the loop’s condition becomes False
, break
forces the loop to stop and moves the execution to the next statement outside the loop. This is useful when we need to terminate a loop early.
for variable in sequence: if condition: break # Stops the loop # Other statements inside the loop while condition: if condition: break # Stops the loop # Other statements inside the loop
break
can be used inside both for
and while
loops.break
, it stops the loop immediately and moves to the next statement outside the loop.1
to 9
.num
reaches 5
, the if
condition becomes True
, and break
stops the loop.1
and increases count
by 1
in each iteration.count
reaches 6
, the condition becomes True
, and break
stops the loop.The break
statement is used to exit loops early when a specific condition is met. It works in both for
and while
loops and helps improve efficiency by avoiding unnecessary iterations. Instead of letting the loop run until its condition becomes False
, break
forces an immediate stop.
.....
.....
.....