Important Notice:

The break Statement

The break Statement

25 views 2 min read
The break Statement :-

break Python का एक Loop Control Statement (लूप नियंत्रण कथन) है। इसका उपयोग किसी for या while loop को तुरंत रोकने (terminate) के लिए किया जाता है।

जब Python को loop के अंदर break statement मिलता है, तो loop वहीं पर समाप्त हो जाता है और loop के बाद लिखा हुआ code execute होने लगता है।

English

The break statement is a Loop Control Statement in Python. It is used to stop or terminate a for or while loop immediately.

When Python encounters a break statement inside a loop, the loop ends immediately, and the program continues with the statement written after the loop.

Syntax:-
break

break को हमेशा किसी loop के अंदर लिखा जाता है।

Example:-
 
for i in range(1, 6):

    if i == 4:
        break

    print(i)

print("Loop End")
 
Output:-
1
2
3
Loop End
 
Because:-

range(1, 6) → 1, 2, 3, 4, 5
 
  • i = 1 → condition False → 1 print होगा। / When i = 1, the condition is False, so 1 is printed.
  • i = 2 → condition False → 2 print होगा। / When i = 2, the condition is False, so 2 is printed.
  • i = 3 → condition False → 3 print होगा। / When i = 3, the condition is False, so 3 is printed.
  • i = 4 → condition True → break execute होगा। / When i = 4, the condition becomes True, so break is executed.
  • break के कारण loop तुरंत समाप्त हो जाएगा। /  The loop stops immediately.
  • इसलिए 4 और 5 print नहीं होंगे। / Therefore, 4 and 5 are not printed.
  • इसके बाद "Loop End" print होगा। / After the loop, "Loop End" is printed.

 

Another Example:- 
 
for i in range(1, 11):

    if i == 6:
        break

    print(i)
 
Output:-
1
2
3
4
5
 
Because:-

जब i की value 6 होती है, तब break execute हो जाता है और loop वहीं समाप्त हो जाता है।
English-
When the value of i becomes 6, the break statement is executed and the loop stops immediately.

break with while Loop :-

break का उपयोग while loop में भी किया जा सकता है।

Example:-
 
i = 1

while i <= 10:

    if i == 5:
        break

    print(i)
    i += 1
 
Output:-
1
2
3
4
 
Because:-

जब i = 5 होता है, तब condition i == 5 True हो जाती है और break loop को समाप्त कर देता है।

English

When i = 5, the condition i == 5 becomes True. Therefore, break terminates the while loop.
 
Use of break Statement :-

break statement का उपयोग मुख्य रूप से निम्न situations में किया जाता है:

  1. Loop को तुरंत रोकने के लिए
    To stop a loop immediately.
  2. किसी particular condition पर loop रोकने के लिए
    To stop the loop when a specific condition is met.
  3. Search करने के बाद loop रोकने के लिए
    To stop searching when the required value is found.
  4. Unnecessary iterations को रोकने के लिए
    To avoid unnecessary iterations.
  5. Infinite loop को रोकने के लिए
    To stop an infinite loop.

Related Notes