Python while loops are one among the various types of loops supported within this language. The while loop keeps on executing a block of code as long as the condition within the while loop remains true. Python while loops can be of great use when you know the exact number of iterations beforehand.
When conditions inside the while loop become false, the execution comes out of the loop and executes the next statement. In this blog, we will learn more about Python While loops and how to use them in programming.
Python While Loop Syntax
The Python While Loops Syntax is similar to other loops with an condition enclosed within the while () bracket. The loop keeps on running until the condition executes to true and if it evaluates to false then we come out of the loop.
while condition:
# Code block to execute |
The Condition here represents a boolean expression which will continue as long as the condition evaluates to “true”.
The code block consists of the statements that are displayed or printed after each iteration of the loop.
Examples of While Loop In Python
Let us take a simple example of the Python While loops to understand the working of the loop.
# Basic while loop example
count = 0 while count < 5: print(“Count is:”, count) count += 1 # Increment count print(“Loop finished.”) |
Output
The while loop starts with a count value as 0 and continue until it is less than 5.
![]() |
2. Write a program to Find a number divisible by 5 or 7 using the while loop.
while True:
number = int(input(“Enter a number: “)) if number % 5 == 0 or number % 7 == 0: print(f”{number} is divisible by 5 or 7.”) break else: print(f”{number} is NOT divisible by 5 or 7. Try again.”) |
Output
![]() |
Read More: 10 Python One-liners that will Boost Your Data Preparation 2025
Flowchart of While Loop In Python
Let us understand the flow of the Python While Loops with this flowchart.
- Step 1: The program starts where the while loop will check whether the condition evaluates to “true” or “false”.
- Step 2:If the condition evaluates to “true” then we proceed ahead in the loop. However, if the “false” then the loop exit and moves to the next part of the program.
- Step 3:If the condition is true then the statements inside the loop are executed.
- Step 4:Once complete, step 1 happens again and the evaluation starts.
- Step 5: The execution keeps on repeating until the condition evaluates to true.
Working of Python While Loops
While loop in Python executes a block of code repeatedly as long as the given condition in the loop evaluates to “true”. Once the condition evaluates to false the loop stops.
- In While loop, before each iteration in the while loop it checks if the condition either evaluates to “true” or “false”.
- If the condition evaluates to “true” then the code block inside the while loop is executed.
- After the block of code makes one execution it then again checks the condition.
- If the condition evaluates to false then the loop terminates and the program continues executing the code after the loop.
Read More: Python Scripting: Automate Your Daily Tasks
Infinite While Loop In Python
This condition in the while loop arises when we fail to give a well defined condition with an endpoint. Hence, the reason the condition never becomes false and the while loop will run indefinitely. You can break out from an infinite loop using the “break” statement.
while True:
print(“This is an infinite loop!”) |
Output
![]() |
You can use the “break” statement in the program to stop the infinite execution of the while loop in Python.
while True:
print(“This is an infinite loop!”) break |
Output
![]() |
The “while true” condition in the loop creates an infinite loop because the true value always evaluates to true. The break statement in the while loop is used to stop the loop after one iteration.
Using Else with Python While Loops
You can easily use the else block with the while loop to define the immediate next statement after the loop complete its execution.
count = 0
while count < 3: print(“Count is:”, count) count += 1 else: print(“Loop finished successfully.”) |
Output
![]() |
Here, you can check the loop executes until the value of count remains less than 3 and after the count variable goes beyond 3 it prints “Loop finished successfully” as the condition now evaluates to false. This is how we can use the else statement in the While loop.
Break & Continue Statement In Python While Loops
You can use the break and continue statement in the while loop based on the condition.
- When we use the break statement the loop exists completely even if the condition evaluates to true.
- When we use continue in the program then the loop skips the current iteration and moves to the next iteration of the loop.
Using Break with the While Loop
count = 0
while count < 10: if count == 5: break # Exit the loop when count equals 5 print(“Count is:”, count) count += 1 |
Output
![]() |
Using Continue with the While Loop
We are taking the same programming loop with continue in place of break statement.
count = 0
while count < 5: count += 1 if count == 3: continue # Skip the print statement when count is 3 print(“Count is:”, count) |
Output
![]() |
Here, due to the continue statement after the 2nd count value where if count value is 3 then the value is skipped.
Does Python Have a Do While Loop?
In Python, we do not have a built-in do while loop as in other programming languages like Java, C, C++ and more. In Python while loop it checks the conditions first. The while loop can be skipped or not executed if the condition is initially false.
Benefits of Using Python While Loops Language
The Python while loops offers many advantages, let us check some of the benefits below.
- We can use the while loop in situations when the number of iterations cannot be predicted beforehand as long as the condition evaluates to true.
- With Python while loops we can implement infinite loops which can be helpful in various applications like servers, real time monitoring and more.
- The while loop supports break, continue and else statements.
- It implements and allows for cleaner and simpler code logic.
- You can control the loop flow carefully to avoid unnecessary iteration in the program.
- It can be used to continuously check for certain events or conditions until a change occurs.
Read More: 10 Python Libraries For Beginners
Learn Python Programming with PW Skills
Become a master in Python development with the knowledge of Data Structures in Python. Get enrolled in PW Skills Decode DSA With Python Course and get in-depth tutorials, practice exercises, real world projects and more with this self paced learning program.
You will learn through industry oriented curriculum and prepare for competitive programming with this course. Get an industry recognised certification from PW Skills after completing the course.
Python While Loops FAQs
Q1. What is while loop in Python?
Ans: Python while loop keeps on executing a block of code as long as the condition within the while loop remains true.
Q2. Can a while loop runs infinitely?
Ans: Yes, when the while loop condition always evaluates to true then the Python while loop runs infinitely.
Q3. Can we use else with While loop in Python?
Ans: Yes, we can use the else conditional statement with the while loop when we need to evaluate a condition after an iteration or complete while loop gets completed.
Q4. How to stop the infinite while loop in Python?
Ans: You can use the break statement in Python to stop the infinite in Python.