While Loop in Python
A while loop in Python is used to execute a block of code repeatedly until a specified condition becomes false.
While loops are very useful when the number of iterations is not fixed and depends on a condition.
Why Use While Loop?
- To execute code repeatedly
- Useful when iteration count is unknown
- Helps automate repetitive tasks
- Widely used in automation frameworks
- Useful in menu-driven programs
Syntax of While Loop
while condition:
# code block
The loop continues until the condition becomes false.
Simple Example of While Loop
count = 1
while count <= 5:
print(count)
count = count + 1
Output
1 2 3 4 5
Understanding the Flow
- Initialize variable
count = 1 - Check condition
count <= 5 - Execute print statement
- Increase count value
- Loop continues until condition becomes false
Infinite While Loop Example
while True:
print("Way2Testing")
The above loop will run forever because the condition is always true.
Using Break Statement
The break statement is used to stop the loop immediately.
count = 1
while True:
print(count)
if count == 5:
break
count = count + 1
Output
1 2 3 4 5
Using Continue Statement
The continue statement skips the current iteration and moves to the next iteration.
count = 0
while count < 5:
count = count + 1
if count == 3:
continue
print(count)
Output
1 2 4 5
While Loop with User Input
password = ""
while password != "admin123":
password = input("Enter Password: ")
print("Login Successful")
Nested While Loop Example
i = 1
while i <= 3:
j = 1
while j <= 2:
print("i =", i, "j =", j)
j = j + 1
i = i + 1
Output
i = 1 j = 1 i = 1 j = 2 i = 2 j = 1 i = 2 j = 2 i = 3 j = 1 i = 3 j = 2
Real-Time Example for Automation Testers
Suppose you want to wait until an API returns status code 200.
status = 500
while status != 200:
print("Retrying API Call...")
# API call logic here
status = 200
print("API Executed Successfully")
Output
Retrying API Call... API Executed Successfully
Difference Between For Loop and While Loop
| For Loop | While Loop |
|---|---|
| Used when iteration count is fixed | Used when iteration count is unknown |
| More readable for collections | Better for condition-based execution |
| Less risk of infinite loops | Higher risk of infinite loops |
| Mostly used with lists, tuples, ranges | Mostly used with conditions |
Common Mistakes in While Loop
- Forgetting to update loop variable
- Creating infinite loops accidentally
- Wrong indentation
- Incorrect loop condition
Best Practices
- Always make sure condition eventually becomes false
- Use meaningful variable names
- Avoid unnecessary infinite loops
- Use break statement carefully
Conclusion
The while loop is one of the most important looping concepts in Python. It helps execute code repeatedly based on conditions and is widely used in real-time applications, automation frameworks, API retries, and menu-driven programs.
Understanding while loops properly will make your Python programming much stronger.
Practice Exercise:
Create a while loop program that prints numbers from 1 to 10 and skips number 5 using continue statement.
No comments:
Post a Comment