Computer Science

Python break and continue Statement | Notes, Flowchart, Examples & Important Programs | CBSE Class 11 Computer Science

Class 11 · Computer Science

Python break Statement

The break statement is a loop control statement used to terminate a loop immediately. Whenever Python encounters a break statement inside a for loop or a while loop, the loop stops executing instantly and the control transfers to the first statement after the loop.


Learning Objectives

  • Understand the purpose of the break statement.
  • Learn how break works inside loops.
  • Use break with both for and while loops.
  • Write programs using the break statement.
  • Understand the behaviour of break in nested loops.

What is the break Statement?

The break statement is used to terminate the execution of a loop before it completes all its iterations.

As soon as Python executes the break statement:

  • The current loop stops immediately.
  • The remaining iterations are skipped.
  • Control moves to the first statement after the loop.

Syntax

break

Working of the break Statement

  1. The loop starts normally.
  2. Each iteration checks the condition inside the loop.
  3. If the break statement is encountered, the loop terminates immediately.
  4. The remaining iterations are skipped.
  5. The program continues with the next statement after the loop.

Flowchart

            Start
              |
        Execute Loop
              |
      break Condition?
         /         \
      Yes          No
       |            |
 Exit the Loop   Continue Loop
       |
 Next Statement

Example 1: Using break with for Loop

for i in range(1,11):
    if i==6:
        break
    print(i)

Output

1
2
3
4
5

Explanation: When the value of i becomes 6, the break statement executes and the loop terminates immediately.


Example 2: Using break with while Loop

i=1
while i<=10:
    if i==5:
        break
    print(i)
    i=i+1

Output

1
2
3
4

Example 3: Stop When User Enters Zero

while True:
    n=int(input("Enter a number (0 to stop): "))
    if n==0:
        break
    print("You entered",n)

Explanation: The loop continues until the user enters 0.


Example 4: Search an Element in a List

numbers=[12,25,40,18,30]
key=18
for n in numbers:
    if n==key:
        print("Element Found")
        break

Output

Element Found

Explanation: Once the required element is found, the loop terminates instead of checking the remaining elements.


Example 5: break in Nested Loops

for i in range(1,4):
    for j in range(1,4):
        if j==2:
            break
        print(i,j)

Output

1 1
2 1
3 1

Explanation: The break statement terminates only the inner loop. The outer loop continues normally.


Dry Run

Program

for i in range(1,6):
    if i==4:
        break
    print(i)
Iteration Value of i Condition (i==4) Output
1 1 False 1
2 2 False 2
3 3 False 3
4 4 True Loop Terminates

Real-Life Applications

  • Searching for a student's roll number.
  • Stopping an ATM transaction when the user cancels.
  • Password verification.
  • Reading a file until a required record is found.
  • Searching products in an inventory.
  • Menu-driven applications.
  • Stopping a game when the player quits.

Advantages of break Statement

  • Improves program efficiency.
  • Stops unnecessary iterations.
  • Reduces execution time.
  • Makes searching operations faster.
  • Useful for menu-driven programs.

Common Programming Mistakes

1. Using break Outside a Loop

Wrong

break

Explanation: The break statement can only be used inside a loop.


2. Expecting break to Stop All Nested Loops

Wrong Assumption:

Many beginners think that break terminates all loops. In reality, it only terminates the loop in which it appears.


3. Writing Statements After break Inside the Same Block

Example

for i in range(5):
    break
    print(i)

Explanation: The print() statement is never executed because the loop terminates before reaching it.


Difference between break and Normal Loop Termination

Normal Loop Using break
Loop completes all iterations. Loop stops immediately.
All values are processed. Remaining values are skipped.
Condition becomes False naturally. Loop is terminated manually.

CBSE Important Programs

  1. Search an element in a list.
  2. Stop printing numbers after a specified value.
  3. Accept numbers until the user enters 0.
  4. Exit a menu-driven program.
  5. Search for a character in a string.
  6. Find the first multiple of 7.
  7. Stop when a negative number is entered.
  8. Terminate a loop after finding the largest value.

Viva Questions

  1. What is the purpose of the break statement?
  2. Can break be used with both for and while loops?
  3. Can break be used outside a loop?
  4. What happens when a break statement executes?
  5. Does break terminate all nested loops?
  6. Can a loop contain multiple break statements?
  7. Give two real-life applications of break.
  8. Why does break improve efficiency?
  9. Can code written after break in the same block execute?
  10. Differentiate between normal loop termination and termination using break.

Exam Tips

  • Use break only when early termination of a loop is required.
  • Remember that break terminates only the current loop.
  • Do not use break outside a loop.
  • Use break to make searching programs more efficient.
  • Trace the program carefully to determine where the loop terminates.

Quick Revision

  • break terminates the current loop immediately.
  • It works with both for and while loops.
  • Remaining iterations are skipped.
  • It terminates only the loop in which it is written.
  • It cannot be used outside a loop.

Python continue Statement

The continue statement is a loop control statement used to skip the remaining statements of the current iteration and immediately move to the next iteration of the loop. Unlike the break statement, which terminates the loop completely, the continue statement keeps the loop running.


Learning Objectives

  • Understand the purpose of the continue statement.
  • Learn how continue works inside loops.
  • Use continue with both for and while loops.
  • Differentiate between break and continue.
  • Write programs using the continue statement.

What is the continue Statement?

The continue statement skips the remaining statements inside the current iteration and transfers control to the next iteration of the loop.

When Python encounters a continue statement:

  • The remaining statements in the current iteration are skipped.
  • The loop is not terminated.
  • The next iteration starts immediately.

Syntax

continue

Working of the continue Statement

  1. The loop starts normally.
  2. The condition inside the loop is evaluated.
  3. If continue is executed, the remaining statements of that iteration are skipped.
  4. The next iteration begins immediately.
  5. The loop continues until it finishes normally.

Flowchart

           Start
             |
      Execute Loop
             |
   continue Condition?
       /           \
     Yes            No
      |              |
Skip Remaining   Execute Remaining
Statements        Statements
      |              |
      +------Next Iteration------+

Example 1: Skip Number 5

for i in range(1,11):
    if i==5:
        continue
    print(i)

Output

1
2
3
4
6
7
8
9
10

Explanation: The value 5 is skipped, while the remaining numbers are printed.


Example 2: Skip Even Numbers

for i in range(1,11):
    if i%2==0:
        continue
    print(i)

Output

1
3
5
7
9

Example 3: Skip Odd Numbers

for i in range(1,11):
    if i%2!=0:
        continue
    print(i)

Output

2
4
6
8
10

Example 4: Using continue with while Loop

i=0
while i<10:
    i=i+1
    if i==6:
        continue
    print(i)

Output

1
2
3
4
5
7
8
9
10

Explanation: The value 6 is skipped, but the loop continues with the remaining values.


Example 5: Skip a Character in a String

name="COMPUTER"
for ch in name:
    if ch=="P":
        continue
    print(ch)

Output

C
O
M
U
T
E
R

Example 6: continue in Nested Loops

for i in range(1,4):
    for j in range(1,4):
        if j==2:
            continue
        print(i,j)

Output

1 1
1 3
2 1
2 3
3 1
3 3

Explanation: The value 2 is skipped only in the inner loop.


Dry Run

Program

for i in range(1,6):
    if i==3:
        continue
    print(i)
Iteration Value of i Condition (i==3) Output
1 1 False 1
2 2 False 2
3 3 True Skipped
4 4 False 4
5 5 False 5

Difference between break and continue

break continue
Terminates the loop immediately. Skips only the current iteration.
Control moves outside the loop. Control moves to the next iteration.
Remaining iterations are not executed. Remaining iterations continue normally.

Real-Life Applications

  • Skip absent students while processing attendance.
  • Ignore invalid records while reading a file.
  • Skip cancelled orders during billing.
  • Ignore duplicate entries in a database.
  • Skip weekends while generating office reports.
  • Filter unwanted data during processing.

Advantages of continue Statement

  • Makes programs more efficient.
  • Avoids unnecessary nested conditions.
  • Improves readability of loop logic.
  • Useful for filtering data.
  • Allows selective processing of records.

Common Programming Mistakes

1. Confusing continue with break

Explanation: continue skips the current iteration, whereas break terminates the loop completely.


2. Forgetting to Update the Loop Variable in while Loop

Wrong

i=1
while i<=5:
    if i==3:
        continue
    print(i)
    i=i+1

Explanation: This creates an infinite loop because i is not updated before executing continue.

Correct

i=1
while i<=5:
    if i==3:
        i=i+1
        continue
    print(i)
    i=i+1

3. Using continue Outside a Loop

Wrong

continue

Explanation: The continue statement can only be used inside a loop.


CBSE Important Programs

  1. Print numbers except a specified number.
  2. Print only odd numbers.
  3. Print only even numbers.
  4. Skip vowels while printing a string.
  5. Skip multiples of 5.
  6. Print numbers except prime numbers (basic logic).
  7. Skip invalid inputs in a loop.
  8. Ignore duplicate values while processing a list.

Viva Questions

  1. What is the purpose of the continue statement?
  2. How is continue different from break?
  3. Can continue be used with both for and while loops?
  4. What happens when continue executes?
  5. Can continue terminate a loop?
  6. Can continue be used outside a loop?
  7. What happens if the loop variable is not updated in a while loop?
  8. Can a loop contain multiple continue statements?
  9. Does continue affect nested loops?
  10. Give any two practical applications of the continue statement.

Exam Tips

  • Remember that continue does not terminate the loop.
  • Use continue when only the current iteration needs to be skipped.
  • Always update the loop variable before continue in a while loop.
  • Do not confuse break with continue.
  • Practice tracing the execution of loops containing continue.

Quick Revision

  • continue skips only the current iteration.
  • The loop continues with the next iteration.
  • It works with both for and while loops.
  • It does not terminate the loop.
  • In a while loop, always update the loop variable before using continue.