After completing this chapter, you will be able to:
- Understand the
whileloop. - Explain why the
whileloop is used. - Write programs using the
whileloop. - Understand the execution flow of a loop.
- Differentiate between
whileandifstatements.
In programming, there are situations where a block of code needs to be executed repeatedly.
For example:
- Printing numbers from 1 to 10.
- Displaying multiplication tables.
- Reading user input until a valid value is entered.
- Processing records repeatedly.
Writing the same statement multiple times is inefficient.
To solve this problem, Python provides the while loop.
A while loop is an iterative control statement that repeatedly executes a block of code as long as the given condition is True.
When the condition becomes False, the loop terminates.
The while loop is used when:
- The number of iterations is not known in advance.
- A condition controls the repetition.
- Continuous execution is required until a condition becomes false.
- ATM PIN Verification
- Password Validation
- Menu-Driven Programs
- Game Loops
- Reading User Input
while condition:
statements Start
│
▼
Initialize
│
▼
Condition
/ \
True False
│ │
▼ ▼
Execute Block Stop
│
▼
Update Variable
│
└──────────────► Condition
- Initialize the loop variable.
- Check the condition.
- If the condition is True, execute the loop body.
- Update the loop variable.
- Repeat the process.
- When the condition becomes False, exit the loop.
Write a Python program to print numbers from 1 to 5 using a while loop.
# Initialize variable
number = 1
# Loop until number becomes greater than 5
while number <= 5:
# Display the current number
print(number)
# Increment the value
number = number + 11
2
3
4
5
START
Initialize number = 1
WHILE number <= 5
Display number
Increment number
END WHILE
STOP
number = 1Initializes the loop control variable.
while number <= 5:Checks whether the value of number is less than or equal to 5.
print(number)Displays the current value of number.
number = number + 1Increments the value of number by 1.
number = 2
while number <= 10:
print(number)
number = number + 22
4
6
8
10
count = 1
while count <= 10:
print("5 x", count, "=", 5 * count)
count = count + 15 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
An Infinite Loop is a loop that never terminates because its condition always remains True.
while True:
print("Welcome")Press Ctrl + C to stop the program manually.
The while loop is used in:
- ATM Machines
- Login Systems
- Password Verification
- Online Games
- Chat Applications
- Menu-Driven Programs
- Executes code repeatedly.
- Reduces code duplication.
- Suitable when the number of iterations is unknown.
- Makes programs efficient and readable.
| if Statement | while Loop |
|---|---|
| Executes once if the condition is True. | Executes repeatedly while the condition is True. |
| Used for decision making. | Used for repetition. |
| No iteration. | Supports iteration. |
❌ Incorrect
number = 1
while number <= 5:
print(number)This creates an infinite loop because number never changes.
✅ Correct
number = 1
while number <= 5:
print(number)
number = number + 1Always ensure the loop condition eventually becomes False.
A while loop is a looping statement that repeatedly executes a block of code as long as the given condition is True.
A while loop should be used when the number of iterations is not known beforehand and execution depends on a condition.
An Infinite Loop is a loop whose condition never becomes False, causing it to execute continuously.
An Infinite Loop can be stopped by:
- Updating the loop variable correctly.
- Changing the loop condition.
- Pressing Ctrl + C during execution.
A while loop is condition-based and is used when the number of iterations is unknown.
A for loop is generally used when the number of iterations is known.
whileis a looping statement.- It executes while the condition is
True. - Initialization, condition, and update are important.
- Forgetting the update may create an infinite loop.
- Used when the number of iterations is unknown.
In this chapter, you learned:
- What a
whileloop is. - Why it is used.
- Syntax and execution flow.
- Practical programs.
- Infinite loop.
- Real-world applications.
- Advantages.
- Common mistakes.
- Interview questions.