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
breakstatement. - Learn how
breakworks inside loops. - Use
breakwith bothforandwhileloops. - Write programs using the
breakstatement. - Understand the behaviour of
breakin 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
- The loop starts normally.
- Each iteration checks the condition inside the loop.
- If the
breakstatement is encountered, the loop terminates immediately. - The remaining iterations are skipped.
- 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
- Search an element in a list.
- Stop printing numbers after a specified value.
- Accept numbers until the user enters 0.
- Exit a menu-driven program.
- Search for a character in a string.
- Find the first multiple of 7.
- Stop when a negative number is entered.
- Terminate a loop after finding the largest value.
Viva Questions
- What is the purpose of the
breakstatement? - Can
breakbe used with bothforandwhileloops? - Can
breakbe used outside a loop? - What happens when a
breakstatement executes? - Does
breakterminate all nested loops? - Can a loop contain multiple
breakstatements? - Give two real-life applications of
break. - Why does
breakimprove efficiency? - Can code written after
breakin the same block execute? - Differentiate between normal loop termination and termination using
break.
Exam Tips
- Use
breakonly when early termination of a loop is required. - Remember that
breakterminates only the current loop. - Do not use
breakoutside a loop. - Use
breakto make searching programs more efficient. - Trace the program carefully to determine where the loop terminates.
Quick Revision
breakterminates the current loop immediately.- It works with both
forandwhileloops. - 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
continuestatement. - Learn how
continueworks inside loops. - Use
continuewith bothforandwhileloops. - Differentiate between
breakandcontinue. - Write programs using the
continuestatement.
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
- The loop starts normally.
- The condition inside the loop is evaluated.
- If
continueis executed, the remaining statements of that iteration are skipped. - The next iteration begins immediately.
- 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
- Print numbers except a specified number.
- Print only odd numbers.
- Print only even numbers.
- Skip vowels while printing a string.
- Skip multiples of 5.
- Print numbers except prime numbers (basic logic).
- Skip invalid inputs in a loop.
- Ignore duplicate values while processing a list.
Viva Questions
- What is the purpose of the
continuestatement? - How is
continuedifferent frombreak? - Can
continuebe used with bothforandwhileloops? - What happens when
continueexecutes? - Can
continueterminate a loop? - Can
continuebe used outside a loop? - What happens if the loop variable is not updated in a
whileloop? - Can a loop contain multiple
continuestatements? - Does
continueaffect nested loops? - Give any two practical applications of the
continuestatement.
Exam Tips
- Remember that
continuedoes not terminate the loop. - Use
continuewhen only the current iteration needs to be skipped. - Always update the loop variable before
continuein awhileloop. - Do not confuse
breakwithcontinue. - Practice tracing the execution of loops containing
continue.
Quick Revision
continueskips only the current iteration.- The loop continues with the next iteration.
- It works with both
forandwhileloops. - It does not terminate the loop.
- In a
whileloop, always update the loop variable before usingcontinue.