break, continue & Nested Loops
Jump statements
Sometimes we need to change the normal flow of a loop — stop early when the answer is found, or skip some values. Python gives three statements for this.
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 does not allow an empty block. pass is a statement that does nothing; use it while planning code.
for i in range(3):
pass # placeholder: write the body later
print('pass lets an empty block run without error')Loop with else
A loop may have an else block that runs only if the loop was not ended by break. It is perfect for "search and report not found" programs, like checking whether a number is 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')We test divisors only up to √n — if 91 had a factor larger than √91 ≈ 9.5, it would also have one smaller than it.
Nested loops
A loop inside another loop is a nested loop. The inner loop completes all its rounds for every single round of the outer loop — like the minute hand going round fully for every hour.
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
Patterns are the best practice for nested loops. Rule: the outer loop counts rows, the inner loop prints the items of one row, and print() after the inner loop moves to the next 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 (inverted)
for i in range(4, 0, -1):
for j in range(i):
print(chr(65 + j), end='')
print()Prime numbers in a range
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 runs forever until a break — the standard way to write menu-driven programs.
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')Summary & practice
breakexits the loop;continueskips to the next round;passdoes nothing.- Loop
elseruns only when nobreakhappened. - Nested loops: inner loop runs fully for each outer round; outer = rows, inner = columns.
Print these patterns:5 4 3 2 1 / 5 4 3 2 / 5 4 3 / 5 4 / 5 and a Floyd's triangle 1 / 2 3 / 4 5 6 / 7 8 9 10. Also print all numbers from 1 to 100 that are divisible by 7 but stop at the first one greater than 60.
