break, continue और Nested Loops
Jump statements
कभी-कभी loop का normal flow बदलना पड़ता है — answer मिलते ही जल्दी रुकना, या कुछ values छोड़ना। Python इसके लिए तीन statements देता है।
break
for n in range(1, 11):
if n == 6:
break # leave the loop immediately
print(n, end=' ')
print('| loop ended')continue
for n in range(1, 11):
if n % 3 == 0:
continue # skip the rest of this round
print(n, end=' ')
print()pass
Python ख़ाली block allow नहीं करता। pass कुछ न करने वाला statement है; code plan करते समय use करें।
for i in range(3):
pass # placeholder: write the body later
print('pass lets an empty block run without error')else वाला loop
Loop के साथ else block हो सकता है जो सिर्फ़ तब चलता है जब loop break से ख़त्म न हुआ हो। "ढूँढो और न मिले तो बताओ" वाले programs के लिए perfect, जैसे number prime है या नहीं।
n = int(input('Enter a number: '))
if n < 2:
print(n, 'is not prime')
else:
for d in range(2, int(n ** 0.5) + 1):
if n % d == 0:
print(n, 'is not prime (divisible by', str(d) + ')')
break
else: # runs only if loop did NOT break
print(n, 'is prime')हम divisors सिर्फ़ √n तक test करते हैं — अगर 91 का √91 ≈ 9.5 से बड़ा factor होता, तो उससे छोटा भी एक होता।
Nested loops
एक loop के अंदर दूसरा loop nested loop है। Outer loop के हर एक round पर inner loop अपने सारे rounds पूरे करता है — जैसे हर घंटे पर minute की सुई पूरा चक्कर लगाती है।
for i in range(1, 4): # outer loop: rows
for j in range(1, 5): # inner loop: columns
print(i * j, end='\t')
print() # new line after each row| j=1 | j=2 | j=3 | j=4 | |
|---|---|---|---|---|
| i=1 | 1 | 2 | 3 | 4 |
| i=2 | 2 | 4 | 6 | 8 |
| i=3 | 3 | 6 | 9 | 12 |
Pattern programs
Nested loops की सबसे अच्छी practice patterns हैं। Rule: outer loop rows गिनता है, inner loop एक row के items print करता है, और inner loop के बाद print() अगली line पर ले जाता है।
1. Right-angled star triangle
rows = 5
for i in range(1, rows + 1):
print('* ' * i)2. Number triangle
rows = 4
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print()3. Pyramid (spaces + stars)
rows = 4
for i in range(1, rows + 1):
print(' ' * (rows - i) + '*' * (2 * i - 1))| Row i | Spaces (4 − i) | Stars (2i − 1) |
|---|---|---|
| 1 | 3 | 1 |
| 2 | 2 | 3 |
| 3 | 1 | 5 |
| 4 | 0 | 7 |
4. Alphabet pattern (उल्टा)
for i in range(4, 0, -1):
for j in range(i):
print(chr(65 + j), end='')
print()Range में prime numbers
print('Primes between 10 and 50:')
for n in range(10, 51):
for d in range(2, n):
if n % d == 0:
break
else:
print(n, end=' ')
print()Menu-driven loop
while True तब तक चलता है जब तक break न मिले — menu-driven programs लिखने का standard तरीका।
while True:
print('1. Square 2. Cube 3. Exit')
ch = input('Choice: ')
if ch == '3':
print('Bye!')
break
x = int(input('Number: '))
if ch == '1':
print('Square =', x * x)
elif ch == '2':
print('Cube =', x ** 3)
else:
print('Wrong choice')सारांश और practice
breakloop से बाहर;continueअगले round पर;passकुछ नहीं करता।- Loop
elseतभी चलता है जबbreakन हुआ हो। - Nested loops: हर outer round पर inner loop पूरा; outer = rows, inner = columns।
ये patterns print करें:5 4 3 2 1 / 5 4 3 2 / 5 4 3 / 5 4 / 5 और Floyd's triangle 1 / 2 3 / 4 5 6 / 7 8 9 10। साथ ही 1 से 100 तक 7 से divisible numbers print करें पर 60 से बड़े पहले number पर रुक जाएँ।
