Python while loop is used to repeat a block of code while a condition is True. It is helpful when we do not know how many times the loop will run.
In this chapter, you will learn about the Python while loop, its working, and how to use it in different situations.
The while loop executes a block of code again and again as long as a given condition remains True. It stops when the condition becomes False. If the condition never becomes False, the loop keeps running and becomes an infinite loop.

In a while loop, we can write one or more statements inside the loop using proper indentation.
Let's see the basic syntax of the while loop in Python:
The while loop checks the given condition before running the code. If the condition is True, the loop executes the code block. After each iteration, the condition is checked again, and the loop continues until the condition becomes False.
Let us take a look at a simple example of a Python while loop.
Output:
0 Hello 1 Hello 2 Hello 3 Hello 4 Hello
Explanation:
In this example, the variable counter starts from 0. The while loop runs as long as the value of counter is less than 5. After each iteration, the value of counter increases by 1, and the loop stops when the condition becomes False.
The image below shows the working of a while loop.

Note: Proper indentation is required in a while loop. Incorrect indentation can cause errors or infinite loops.
In the following examples, we will see how to use the while loop in different situations to solve various problems.
In this example, we are finding numbers between 1 and 50 that are divisible by 5 or 7 using a while loop.
Output:
5 7 10 14 15 20 21 25 28 30 35 40 42 45 49
In this example, we are calculating the sum of squares of the first 15 natural numbers using a while loop.
Output:
The sum of squares is 1240
In this example, we are checking whether numbers in a list are prime using a while loop.
Output:
34 is not a PRIME number 12 is not a PRIME number 54 is not a PRIME number 23 is a PRIME number 75 is not a PRIME number 34 is not a PRIME number 11 is a PRIME number
In this example, we are checking whether a number is an Armstrong number using a while loop.
Output:
It is an Armstrong number
In this example, we are creating a multiplication table using a while loop.
Output:
The Multiplication Table of: 21 21 x 1 = 21 21 x 2 = 42 21 x 3 = 63 21 x 4 = 84 21 x 5 = 105 21 x 6 = 126 21 x 7 = 147 21 x 8 = 168 21 x 9 = 189 21 x 10 = 210
An infinite while loop occurs when the condition always remains True that causes the loop to run continuously without stopping. Such loops do not end on their own and require manual interruption.
In the following example, we are creating an infinite loop because the condition always remains True.
Output:
Infinite Loop Infinite Loop Infinite Loop Infinite Loop ......
Explanation:
In this example, the variable age is set to 28. The condition age > 19 is always True, so the loop keeps running and printing the message again and again. Since the condition never becomes False, the loop does not stop unless we manually terminate the program.
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
We will now look at various loop control statements used in Python's while loop.
The break statement immediately terminates the while loop, regardless of the condition.
Output:
Counter is: 0 Counter is: 1 Counter is: 2 Counter is: 3 Breaking the loop at 4
Explanation:
In the above example, the loop stops as soon as the counter reaches 4, even though the original condition was counter < 8.
The continue statement skips the current iteration and moves to the next one without executing the remaining code inside the loop.
Output:
Counter is: 1 Counter is: 2 Counter is: 3 Counter is: 5 Counter is: 6 Counter is: 7 Counter is: 8
Explanation:
In the above example, the loop skips the print statement when the counter value is 4; however, it continues executing afterward.
The pass Statement acts as a null operator or placeholder. It is applied when a statement is required syntactically, which means that the statement will be visible in the code but will be ignored; hence, it returns nothing.
Output:
Counter is: 0 Counter is: 1 Counter is: 2 Counter is: 3 Counter is: 4 Counter is: 5 Counter is: 6 Counter is: 7
Explanation:
In this example, the pass statement ensures that Python does not throw an error while we develop the code. Since this function is empty and returns nothing due to the pass statement, there will be no output when we call this function.
The else block executes after the loop finishes normally (without a break statement).
Output:
Counter is: 0 Counter is: 1 Counter is: 2 Counter is: 3 Counter is: 4 Counter is: 5 Counter is: 6 Counter is: 7 Loop completed successfully!
The Break statement with the else-while loop terminates the program at that very instance when it is executed. In this case, the loop terminates immediately, and the else block is skipped.
Output:
Counter is: 0 Counter is: 1 Counter is: 2 Counter is: 3 Counter is: 4
Explanation:
In this example, the else block does not execute as the break statement stops the loop early.
We request you to subscribe our newsletter for upcoming updates.