Python Loops | while Loop, for Loop, range(), break, continue and pass | Complete Notes | CBSE Class 11 Informatics Practices (2026–27)
Class 11 · Informatics Practices
Python Loops | while Loop, for Loop, range(), break, continue and pass
In many situations, a programmer needs to execute the same set of statements multiple times. Writing the same code repeatedly makes the program lengthy, difficult to understand, and harder to maintain. Python provides loops to solve this problem. A loop repeatedly executes a block of statements until a specified condition becomes false or a sequence is completely processed.
Loops are one of the most powerful features of Python and are widely used in applications such as displaying reports, processing records, generating tables, validating user input, and performing repetitive calculations.
Learning Outcomes
After studying this chapter, you will be able to:
- Understand the concept of looping.
- Differentiate between while and for loops.
- Use the range() function.
- Write programs using loops.
- Understand nested loops.
- Control loop execution using break, continue, and pass.
What is a Loop?
A loop is a control structure that repeatedly executes a block of statements as long as a specified condition remains true or until all items in a sequence have been processed.
Why Do We Need Loops?
Suppose you want to display the message "Welcome to Python" ten times.
Without a loop:
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
Using a loop:
for i in range(10):
print("Welcome to Python")
Using loops makes the program shorter, easier to understand, and easier to maintain.
Advantages of Loops
- Reduce the amount of code.
- Avoid repetition.
- Improve readability.
- Save programming time.
- Make programs easier to modify.
Types of Loops in Python
| Loop | Used When |
|---|---|
| while | Number of iterations is not known in advance. |
| for | Number of iterations or sequence is known. |
The while Loop
The while loop repeatedly executes a block of statements as long as the specified condition evaluates to True.
Syntax of while Loop
while condition:
statement(s)
Flowchart of while Loop
Condition
|
+-----+------+
| |
True False
|
Execute Block
|
Update Condition
|
+------------+
Example 1: Print Numbers from 1 to 5
count = 1
while count <= 5:
print(count)
count = count + 1
Output
1
2
3
4
5
Working of the Program
| Iteration | Value of count | Condition | Output |
|---|---|---|---|
| 1 | 1 | True | 1 |
| 2 | 2 | True | 2 |
| 3 | 3 | True | 3 |
| 4 | 4 | True | 4 |
| 5 | 5 | True | 5 |
| 6 | 6 | False | Loop Stops |
Example 2: Multiplication Table
num = int(input("Enter a number: "))
i = 1
while i <= 10:
print(num, "x", i, "=", num*i)
i = i + 1
Infinite Loop
If the condition of a while loop never becomes False, the loop executes forever. Such a loop is called an infinite loop.
Example
while True:
print("Python")
Note: Avoid creating infinite loops unless they are intentionally required.
The for Loop
The for loop is used to iterate over a sequence such as a string, list, tuple, dictionary, or the sequence generated by the range() function.
Syntax of for Loop
for variable in sequence:
statement(s)
Flowchart of for Loop
Start
|
Next Item
|
Execute Block
|
More Items?
|
Yes --------+
|
No
|
End
Example 3: Print Numbers from 1 to 5
for i in range(1,6):
print(i)
Output
1
2
3
4
5
Example 4: Display Student Names
students = ["Aarav","Diya","Riya","Kabir"]
for name in students:
print(name)
Output
Aarav
Diya
Riya
Kabir
Example 5: Iterate Through a String
for ch in "Python":
print(ch)
Output
P
y
t
h
o
n
The range() Function
The range() function generates a sequence of numbers. It is commonly used with the for loop.
Forms of range()
| Function | Description |
|---|---|
| range(stop) | Generates numbers from 0 to stop − 1. |
| range(start, stop) | Generates numbers from start to stop − 1. |
| range(start, stop, step) | Generates numbers with the specified step value. |
Example 6: range(stop)
for i in range(5):
print(i)
Output
0
1
2
3
4
Example 7: range(start, stop)
for i in range(3,8):
print(i)
Output
3
4
5
6
7
Example 8: range(start, stop, step)
for i in range(2,21,2):
print(i)
Output
2
4
6
8
10
12
14
16
18
20
Nested Loops
A loop placed inside another loop is known as a nested loop. The inner loop executes completely for every iteration of the outer loop.
Example
for i in range(1,4):
for j in range(1,4):
print(i,j)
Loop Control Statements
Sometimes, while executing a loop, we may need to stop the loop immediately, skip certain iterations, or keep a placeholder for future code. Python provides three loop control statements for this purpose:
- break
- continue
- pass
The break Statement
The break statement immediately terminates the loop when it is executed. The control of the program moves to the first statement after the loop.
Syntax of break
for variable in sequence:
if condition:
break
statement(s)
Flowchart of break Statement
Start Loop
|
Condition True?
/ \
Yes No
| |
break Execute Block
| |
+-------> Exit Loop
Example 1: Stop the Loop at 5
for i in range(1,11):
if i == 5:
break
print(i)
Output
1
2
3
4
When i becomes 5, the break statement is executed, and the loop terminates immediately.
Real-Life Example
An ATM machine allows three attempts to enter the correct PIN. Once the correct PIN is entered, the verification loop stops immediately using the break statement.
The continue Statement
The continue statement skips the remaining statements of the current iteration and transfers control to the next iteration of the loop.
Syntax of continue
for variable in sequence:
if condition:
continue
statement(s)
Flowchart of continue Statement
Start Loop
|
Condition True?
/ \
Yes No
| |
continue Execute Block
| |
+-------> Next Iteration
Example 2: 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
The value 5 is skipped, but the loop continues with the remaining iterations.
Real-Life Example
A school management system processes the attendance of all students. If a student's record is incomplete, the system skips that record using continue and processes the next student's data.
The pass Statement
The pass statement is a null statement. It does nothing and is used as a placeholder where a statement is syntactically required but no action needs to be performed.
Syntax of pass
if condition:
pass
Example 3: Using pass
for i in range(1,6):
if i == 3:
pass
print(i)
Output
1
2
3
4
5
The pass statement performs no operation. The program continues executing normally.
Why is pass Useful?
- Acts as a placeholder during program development.
- Prevents syntax errors when a code block is intentionally left empty.
- Allows developers to write incomplete programs without causing execution errors.
Comparison of Loop Control Statements
| Statement | Purpose | Effect on Loop |
|---|---|---|
| break | Terminates the loop immediately. | Loop stops completely. |
| continue | Skips the current iteration. | Loop continues with the next iteration. |
| pass | Does nothing. | Loop executes normally. |
Comparison of while Loop and for Loop
| while Loop | for Loop |
|---|---|
| Used when the number of iterations is not known. | Used when the number of iterations is known. |
| Condition-controlled loop. | Sequence-controlled loop. |
| Requires manual initialization and updation. | Initialization and iteration are handled automatically. |
| Suitable for validation and menu-driven programs. | Suitable for traversing sequences such as strings, lists, and ranges. |
Common Programming Errors
| Mistake | Correct Practice |
|---|---|
| Forgetting to update the loop variable in a while loop. | Always update the loop variable to avoid infinite loops. |
| Using break instead of continue. | Use break to terminate the loop and continue to skip an iteration. |
| Incorrect indentation inside the loop. | Maintain proper indentation for all loop statements. |
| Using an incorrect range() value. | Remember that the stop value is not included. |
| Creating an infinite loop accidentally. | Ensure that the loop condition eventually becomes false. |
Interview Corner
Q. What is the difference between break and continue?
Answer: The break statement immediately terminates the loop, whereas the continue statement skips the remaining statements of the current iteration and proceeds with the next iteration.
Quick Revision
| Concept | Remember |
|---|---|
| Loop | Repeats a block of statements. |
| while | Condition-controlled loop. |
| for | Sequence-controlled loop. |
| range() | Generates a sequence of numbers. |
| break | Terminates the loop immediately. |
| continue | Skips the current iteration. |
| pass | Null statement used as a placeholder. |
| Nested Loop | A loop inside another loop. |
Important Points to Remember
- Loops help avoid writing repetitive code.
- The while loop is used when the number of iterations is not known in advance.
- The for loop is generally used to iterate over a sequence.
- The range() function generates a sequence of numbers.
- The stop value in range() is always excluded.
- break terminates the loop immediately.
- continue skips the current iteration.
- pass performs no action and is mainly used as a placeholder.
CBSE Exam Tips
- Understand the difference between while and for loops.
- Practice all three forms of the range() function.
- Learn the behavior of break, continue, and pass with examples.
- Trace loop execution step by step to determine the output.
- Pay attention to indentation while writing loop programs.
- Practice writing programs for tables, factorials, sums, and pattern generation using loops.
Summary
Loops are essential control structures that allow a program to execute a block of statements repeatedly. Python provides the while loop for condition-based repetition and the for loop for iterating over sequences. The range() function simplifies numeric iteration, while the break, continue, and pass statements provide additional control over loop execution. A strong understanding of loops enables students to write efficient, concise, and logical programs, making them an indispensable part of Python programming.