Python while loop

The while loop in Python is used when you want to repeat a certain block of code for a given number of times. The loops are the key concept of any programming language as they make our code more cleaner and easier. Python provides us the while and the for loop.


Why to Use Loops?

Let’s understand why loops are so important. Suppose someone gives you a task to print numbers from 1 to 5. What will you do? Simple, you will use print() function 5 times to print each number. But what if you have to print numbers from 1 to 100 or maybe up to 1000. You can not repeat print() function 100 or 1000 times.

Loops help us in such situations. A loop can be repeated as many times as you want. So to print numbers from 1 to 100 or 1000 you can use a loop. It will repeat itself again and again.

Python provides us the while and the for loop. In this tutorial, we will learn everything about the while loop. So Let’s get started.


Syntax of the while Loop

while condition:
    body of the loop

At first, the loop checks the given condition. If the condition evaluates to True, the code inside the while loop is executed for the first time. After executing the code, the while loop again checks the given condition. If it again evaluates to True, the code inside the loop is again executed. This process keeps on repeating until the condition evaluates to False. Refer the image below:

Python while loop

Note: The body of the loop should be in proper indentation.


Example 1:

# A program to demonstrate the while loop in Python

count = 1
while count<=5:
    print('Welcome to Programmers Portal.')
    count = count + 1

Output:

Welcome to Programmers Portal.
Welcome to Programmers Portal.
Welcome to Programmers Portal.
Welcome to Programmers Portal.
Welcome to Programmers Portal.

Example 2:

# A program to print numbers from 1 to 10 using while loop

num = 1
while num<=10:
    print(num)
    num = num + 1

Output:

1
2
3
4
5
6
7
8
9
10

Python while Loop With else Statement

In Python, it is also possible to use an else statement with a while loop. The else statement is executed only when the condition in the while loop evaluates to False. Refer below example:

# A program to demonstrate the while loop with else statement.

count = 1
while count<=5:
    print('count less than 5.')
    count = count + 1
else:
    print('Now count is greater than 5.')

Output:

count less than 5.
count less than 5.
count less than 5.
count less than 5.
count less than 5.
Now count is greater than 5.

In the above program, we used a count variable which has an initial value as 1. The loop ran 5 times as the count value was less than 5. After 5th iteration, the count value became 6 and the condition evaluated to False. Hence the else block was executed.


The Infinite While Loop

A loop is called an infinite loop if the condition provided in the loop never evaluates to False. In such a case, the loop keeps on repeating itself again, and again and it never stops. It will keep on repeating until the memory runs out of space. Refer below example:

# A program to explain the infinite loop in Python

num = 1
while num!=2:
    print('This is an infinite loop.')

Output:

This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
....
....

In the above program, as the value of variable num is 1. Therefore, the expression num!=2 always evaluates to True, and hence the loop keeps on repeating infinite times.

Note: You can use CTRL+ C to exit the the infinite loop.


The break Statement

The break statement is used to stop the loop even if the condition is True. The break statement becomes useful when we want to terminate the loop for some external condition.

# A program to demonstrate the break statement in Python with while loop

num = 1
while num<=10:
    if num == 5: # stop the loop if num is 5
        break
    print(num)
    num = num + 1

Output:

1
2
3
4

The continue Statement

The continue statement is used in situations where we don’t want the loop to be executed for a given condition. However, the loop will keep on executing normally for the rest of the conditions. Once that given condition matches, the loop skips the execution. Refer below example:

# A program to demonstrate the continue statement in Python

num = 0
while num<10:
    num = num + 1
    if num == 5:    # If num is 5 skip the execution
        continue
    print(num)
    

Output:

1
2
3
4
6
7
8
9
10

In the output above, number 5 is missing. This is because at num == 5 the loop skips the execution.