While Loop in Python | What is the Use of While Loop in Python

While loop in Python is used to repeat a specific block of code until a given condition gets satisfied. This means the condition is evaluated and if the condition is True, then the block of code or statements within the loop gets executed. This happens until the condition becomes False. Once the condition becomes False, the control returns to the next statement outside the loop.

flowchart while loop in python

We generally use a while loop when the number of iterations is unknown.

Syntax for while loop in Python

while (condition):
    statement (s)

Here, the condition can be any expression and executable statements can be of a single statement or multiple statements. Also, the body of the while loop (statements) is identified using indentation. In Python, all the statements indented by the same number of spaces are considered to be part of a single block of code. 

While Loop Examples

a) While loop in a list

Let’s now look at how to print the first two elements of a List in Python using the while loop. 

#printing the first two elements of a list
list = [40, "FACEPrep", 30.45]
i = 0 
while(i < 2):
    print (list[i])  #printing the element in the index i
    i = i + 1
Output:
40
FACEPrep
Note: When working with the while loop, it is important to declare the indexing variable (i) beforehand and to increment the indexing variable accordingly. Else it will result in an infinite loop.

b) While loop using numbers

Now, consider a program where you want to print out the squares of all the numbers less a particular number. Let’s see how this works with a while statement.
#printing the square of numbers less than n
i = 1
while (i <= 10):
    print (i ** 2) #printing the element in the index i
    i = i + 1
Output:
1
4
9
16
25
36
49
64
81
100

c) While loop using else

Let now take a similar program as above – Write a program to check if the given number is even or odd. If even, then print the square of it and if odd, print the number itself.

#printing the square of numbers less than n
i = 1
while i < 10:
    if (i % 2 == 0):
        print(i ** 2)
        i = i + 1
    else:
        print(i)
        i = i + 1
Output:
1
4
3
16
5
36
7
64
9

d) break in while loop 

Using the break statement we can stop the execution of the while loop even if the condition is True. To understand this, let’s look at the below program
list = [40, "FACEPrep", 30.45, 50, 67.23, "Python"]
i = 0
while(i < 6):
    print(list[i])
    i = i + 1
    if(i == 3):
        break
Output:
40
FACEPrep
30.45
Explanation: Here, the while loop runs until the value of the variable i is less than 6. But because of the break statement, the loop gets terminated when the value of the variable i is 3.

e) continue in a while loop 

The continue statement can be used to stop the current iteration and to begin the next iteration. For example, let’s say you want to print all the odd numbers less than a particular value. Here is how you can do it using continue keyword in Python.
#printing odd numbers less than n
i = 1
while (i < 10):
if (i % 2 == 0):
i = i + 1
continue
else:
print (i)
i = i + 1
Output:
1
3
5
7
9

Here, the continue statement gets executed when the value of the variable is an even number. This simply means, whenever it is an even number, we simply skip all other statements and execute the next iteration.

While loop in Python FAQs

A while loop is ideal for iteration when the number of iterations is not known. Also, it is ideal to use a while loop when you have a condition that needs to be satisfied.

A loop becomes an infinite loop if the while condition never becomes FALSE. Such a loop that never terminates is called an infinite loop.

No. There is no do while loop in Python.

Usually, the control shifts out of the while loop when the while condition is no False. Or you can use various control statements like break, continue, etc to break out of the while loop or break of the particular iteration of the while loop.

You can write an empty while function in Python using the pass statements. Here is how you can write it.

#empty while statement in python
i = 0
while (i < 10) :
pass