Python Break and Continue Statements
What are Break and Continue Statements?
In Python loops, break
and continue
are special statements used to control the flow inside loops.
- break: Immediately terminates the loop and moves control outside the loop.
- continue: Skips the current iteration and moves to the next iteration of the loop.
These help in controlling loops efficiently based on specific conditions.
Python के loops में break
और continue
statements flow control के लिए उपयोग होते हैं।
- break: loop को तुरंत रोक देता है और control loop के बाहर भेजता है।
- continue: current iteration को छोड़कर अगले iteration पर चला जाता है।
ये statements loop को बेहतर तरीके से नियंत्रित करने में मदद करते हैं।
When to Use Break and Continue?
- Use
break
when you want to stop the entire loop based on a condition. - Use
continue
when you want to skip certain iterations but continue the loop. - Both are useful to avoid unnecessary processing inside loops.
- They improve code efficiency and readability.
- जब आपको किसी condition पर पूरा loop रोकना हो तो
break
का उपयोग करें। - जब कुछ iterations को छोड़कर loop को जारी रखना हो तो
continue
का उपयोग करें। - दोनों loops के अंदर अनावश्यक processing से बचाने के लिए उपयोगी हैं।
- ये कोड की दक्षता और पठनीयता बढ़ाते हैं।
10 Examples of Break Statement
Example 1: Simple Break in While Loop
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1
Loop stops when i becomes 5, so numbers 1 to 4 are printed.
जब i 5 होता है तो loop रुक जाता है, इसलिए 1 से 4 तक print होगा।
Output:
2
3
4
Example 2: Break in For Loop
for num in range(1, 11):
if num == 7:
break
print(num)
For loop breaks when num is 7, printing numbers 1 to 6.
जब num 7 होता है तो for loop रुक जाता है, इसलिए 1 से 6 तक print होगा।
Output:
2
3
4
5
6
Example 3: Break to Exit Infinite Loop
while True:
user_input = input('Enter exit to stop: ')
if user_input == 'exit':
break
print('Loop exited')
Infinite loop ends when user types 'exit'.
असीमित loop तब बंद होगा जब user 'exit' टाइप करेगा।
Output:
Example 4: Nested Loop Break
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
break
print(f'i={i}, j={j}')
Inner loop breaks when j is 2; outer loop continues.
जब j 2 होता है inner loop रुक जाता है, outer loop चलता रहता है।
Output:
i=2, j=1
i=3, j=1
Example 5: Break with Condition in Loop
numbers = [1, 3, 5, 7, 8, 9]
for n in numbers:
if n % 2 == 0:
break
print(n)
Loop breaks on first even number (8).
पहली even संख्या (8) पर loop रुक जाता है।
Output:
3
5
7
Example 6: Break with User Input in For Loop
for i in range(5):
inp = input('Enter stop to exit: ')
if inp == 'stop':
break
print(f'You entered: {inp}')
User can break the loop by entering 'stop'.
User 'stop' लिखकर loop रोक सकता है।
Output:
Example 7: Break in While Loop with Else
i = 0
while i < 3:
if i == 2:
break
print(i)
i += 1
else:
print('Completed without break')
Else block does not execute due to break.
Break होने की वजह से else block execute नहीं होगा।
Output:
1
Example 8: Break in Loop Searching an Item
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
if fruit == 'banana':
print('Banana found!')
break
Breaks the loop when banana is found.
जब banana मिले तो loop रुक जाता है।
Output:
Example 9: Break in While Loop Based on Counter
count = 0
while True:
print(count)
count += 1
if count == 3:
break
Infinite loop broken after printing 3 numbers.
असीमित loop 3 नंबर print करने के बाद रुक जाता है।
Output:
1
2
Example 10: Break to Optimize Search
nums = [10, 20, 30, 40, 50]
search = 30
for n in nums:
if n == search:
print('Found', n)
break
Stops loop once the item is found.
item मिलने के बाद loop रोक देता है।
Output:
10 Examples of Continue Statement
Example 1: Skip Even Numbers
for i in range(1, 6):
if i % 2 == 0:
continue
print(i)
Continue skips even numbers, printing only odd numbers.
continue even नंबर को skip कर odd नंबर print करता है।
Output:
3
5
Example 2: Continue in While Loop
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
Skips printing 3 in while loop using continue.
while loop में continue से 3 print नहीं होगा।
Output:
2
4
5
Example 3: Continue to Skip Blank Lines
lines = ['Hi', '', 'Hello', '', 'Bye']
for line in lines:
if line == '':
continue
print(line)
Continue skips blank lines in list.
list में खाली lines को continue skip करता है।
Output:
Hello
Bye
Example 4: Continue to Skip Certain Inputs
while True:
data = input('Enter data (type quit to stop): ')
if data == 'quit':
break
if data == 'skip':
continue
print(f'You entered: {data}')
Skips printing when user types 'skip', breaks on 'quit'.
'skip' input मिलने पर print नहीं करता, 'quit' पर loop रुकता है।
Output:
Example 5: Continue in Nested Loops
for i in range(1, 3):
for j in range(1, 4):
if j == 2:
continue
print(f'i={i}, j={j}')
Skips j=2 in inner loop using continue.
inner loop में j=2 को continue से skip करता है।
Output:
i=1, j=3
i=2, j=1
i=2, j=3
Example 6: Continue to Filter Characters
text = 'Hello123'
for char in text:
if not char.isalpha():
continue
print(char)
Continue skips non-alphabet characters.
non-alphabet characters को continue से skip करता है।
Output:
e
l
l
o
Example 7: Continue with Multiple Conditions
for i in range(1, 10):
if i % 2 == 0 or i % 3 == 0:
continue
print(i)
Skips numbers divisible by 2 or 3.
2 या 3 से divisible नंबर को continue skip करता है।
Output:
5
7
11
Example 8: Continue to Skip Error Inputs
while True:
try:
num = int(input('Enter a number: '))
except ValueError:
print('Invalid input')
continue
print(f'You entered: {num}')
break
Continue restarts loop if input is invalid number.
गलत input पर continue से loop फिर शुरू होता है।
Output:
Example 9: Continue to Filter List Elements
items = [1, 0, 3, 0, 5]
for item in items:
if item == 0:
continue
print(item)
Skips zero elements in list using continue.
list में zero elements को continue skip करता है।
Output:
3
5
Example 10: Continue in String Processing
s = 'Python!'
for ch in s:
if ch == '!':
continue
print(ch)
Continue skips printing the exclamation mark.
exclamation mark को continue skip करता है।
Output:
y
t
h
o
n