Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔁 Unit 2 – Flow of Control & Functions  ·  Lesson 17

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.

⛔
breakExit the loop completely.
⏭️
continueSkip to the next iteration.
⏸️
passDo nothing (placeholder).
Jump statements

break

Python
for n in range(1, 11):
    if n == 6:
        break             # leave the loop immediately
    print(n, end=' ')
print('| loop ended')
1 2 3 4 5 | loop ended
123456break78910
The loop stops at 6; 6 to 10 are never printed

continue

Python
for n in range(1, 11):
    if n % 3 == 0:
        continue          # skip the rest of this round
    print(n, end=' ')
print()
1 2 4 5 7 8 10
12345678910
Multiples of 3 are skipped, but the loop keeps going

pass

Python does not allow an empty block. pass is a statement that does nothing; use it while planning code.

Python
for i in range(3):
    pass        # placeholder: write the body later
print('pass lets an empty block run without error')
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.

Python
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')
Enter a number: 91 91 is not prime (divisible by 7)

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.

Python
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
1 2 3 4 2 4 6 8 3 6 9 12
j=1j=2j=3j=4
i=11234
i=22468
i=336912
Outer loop i makes rows, inner loop j makes columns: 3 × 4 = 12 values

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

Python
rows = 5
for i in range(1, rows + 1):
    print('* ' * i)
* * * * * * * * * * * * * * *

2. Number triangle

Python
rows = 4
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()
1 1 2 1 2 3 1 2 3 4

3. Pyramid (spaces + stars)

Python
rows = 4
for i in range(1, rows + 1):
    print(' ' * (rows - i) + '*' * (2 * i - 1))
* *** ***** *******
Row iSpaces (4 − i)Stars (2i − 1)
131
223
315
407
Find the formula for spaces and stars in each row

4. Alphabet pattern (inverted)

Python
for i in range(4, 0, -1):
    for j in range(i):
        print(chr(65 + j), end='')
    print()
ABCD ABC AB A

Prime numbers in a range

Python
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()
Primes between 10 and 50: 11 13 17 19 23 29 31 37 41 43 47

while True runs forever until a break — the standard way to write menu-driven programs.

Python
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')
1. Square 2. Cube 3. Exit Choice: 1 Number: 9 Square = 81 1. Square 2. Cube 3. Exit Choice: 2 Number: 4 Cube = 64 1. Square 2. Cube 3. Exit Choice: 3 Bye!

Summary & practice

  • break exits the loop; continue skips to the next round; pass does nothing.
  • Loop else runs only when no break happened.
  • Nested loops: inner loop runs fully for each outer round; outer = rows, inner = columns.
✅ Practice

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.

Frequently Asked Questions

What is the difference between break and continue?
break ends the whole loop immediately. continue only skips the remaining statements of the current iteration and moves to the next iteration.
When does the else part of a loop run?
The else block of a for or while loop runs when the loop finishes normally, that is, without meeting a break. It is useful for search programs like checking primes.
How many times does the inner loop run in a nested loop?
For every single iteration of the outer loop, the inner loop runs completely. So an outer loop of 3 and an inner loop of 4 run the inner body 3 × 4 = 12 times.
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicUser-defined Functions Your progress: 17 / 29 (59%) · Lesson 18 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free