Python while Loop | Notes, Syntax, Flowchart, Examples & Important Programs | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python while Loop
The while loop is an iterative statement used to execute a block of code repeatedly as long as a given condition remains True. Unlike the for loop, which is generally used when the number of iterations is known, the while loop is preferred when the number of iterations is not known in advance.
Learning Objectives
- Understand the concept of the
whileloop. - Learn the syntax and working of the
whileloop. - Write programs using the
whileloop. - Understand infinite loops and nested
whileloops. - Differentiate between
forandwhileloops.
What is a while Loop?
A while loop repeatedly executes a block of statements as long as the specified condition evaluates to True. Before every iteration, Python checks the condition. If the condition becomes False, the loop terminates.
The while loop is also known as an entry-controlled loop because the condition is tested before entering the loop body.
Syntax
while condition:
statements
Working of the while Loop
- The condition is evaluated.
- If the condition is True, the loop body executes.
- The control returns to the condition.
- The process continues until the condition becomes False.
- When the condition is False, the loop terminates.
Flowchart
Start
|
Initialize Variable
|
Check Condition?
/ \
True False
| |
Execute Statements End
|
Update Variable
|
+-------------+
Example 1: Print Numbers from 1 to 5
i=1
while i<=5:
print(i)
i=i+1
Output
1
2
3
4
5
Example 2: Print Even Numbers
i=2
while i<=10:
print(i)
i=i+2
Example 3: Print Odd Numbers
i=1
while i<=9:
print(i)
i=i+2
Example 4: Reverse Counting
i=10
while i>=1:
print(i)
i=i-1
Example 5: Multiplication Table
n=int(input("Enter a number: "))
i=1
while i<=10:
print(n,"x",i,"=",n*i)
i=i+1
Example 6: Sum of First 10 Natural Numbers
sum=0
i=1
while i<=10:
sum=sum+i
i=i+1
print("Sum =",sum)
Dry Run
Program
i=1
while i<=3:
print(i)
i=i+1
| Iteration | Value of i | Condition | Output |
|---|---|---|---|
| 1 | 1 | True | 1 |
| 2 | 2 | True | 2 |
| 3 | 3 | True | 3 |
| 4 | 4 | False | Loop Stops |
Infinite while Loop
If the condition never becomes False, the loop continues forever. Such a loop is called an Infinite Loop.
Example
while True:
print("Welcome")
Explanation: Since the condition is always True, the loop never terminates unless interrupted manually.
Nested while Loop
A Nested while Loop means placing one while loop inside another while loop.
Example
i=1
while i<=3:
j=1
while j<=2:
print(i,j)
j=j+1
i=i+1
Output
1 1
1 2
2 1
2 2
3 1
3 2
Real-Life Applications
- ATM transactions until the user exits.
- Menu-driven programs.
- Password verification.
- Reading sensor values continuously.
- Online gaming loops.
- Processing user input until a valid value is entered.
- Inventory management systems.
Difference between for Loop and while Loop
| Feature | for Loop | while Loop |
|---|---|---|
| Iterations | Usually known | Usually unknown |
| Condition | Based on a sequence | Based on a Boolean condition |
| Initialization | Handled automatically | Done manually |
| Updation | Automatic | Programmer must update |
Common Programming Mistakes
1. Forgetting to Update the Variable
Wrong
i=1
while i<=5:
print(i)
Explanation: This creates an infinite loop because i never changes.
2. Incorrect Indentation
Wrong
while i<=5:
print(i)
Correct
while i<=5:
print(i)
3. Wrong Condition
Example
i=10
while i<=1:
print(i)
i=i-1
Explanation: The loop will never execute because the condition is initially False.
CBSE Important Programs
- Print numbers from 1 to N.
- Print numbers from N to 1.
- Print even numbers.
- Print odd numbers.
- Find the sum of first N natural numbers.
- Display multiplication table.
- Reverse counting.
- Print squares of numbers.
- Print cubes of numbers.
- Accept input until the user enters 0.
Viva Questions
- What is a
whileloop? - Why is it called an entry-controlled loop?
- When should a
whileloop be preferred over aforloop? - What is an infinite loop?
- How can an infinite loop be avoided?
- What is a nested
whileloop? - Why is variable updation important?
- Can a
whileloop execute zero times? - Can a
whileloop contain another loop? - Give any two real-life applications of the
whileloop.
Exam Tips
- Use a
whileloop when the number of iterations is not known. - Always update the loop variable inside the loop.
- Maintain proper indentation.
- Be careful with the loop condition to avoid infinite loops.
- Trace the program manually to understand loop execution.
Quick Revision
- The
whileloop repeats statements while the condition is True. - It is an entry-controlled loop.
- The condition is checked before every iteration.
- The loop variable must be updated manually.
- If the condition never becomes False, an infinite loop occurs.