Python While Loop


In this section we will learn about the while loop in Python that how it works, syntax of while loop and examples of while loop.

The while loop is used when the number of iterations are not known in advance, the while loop is terminated as soon as the condition becomes false. This loop executes one or more statements while the given condition remains true. It is useful where the numbers of iterations are not known in advance.

 

Syntax of while loop

The following is the syntax of while loop:

while condition:

      body of the loop

First of all, the condition is evaluated. If it is true, the control enters the body of the loop and executes all statements in the body. After executing the statements it again moves to the start of loop and check the condition. This process continues as long as the condition remains true. When the condition becomes false, the loop is terminated. While loop terminates only when the condition becomes false if the condition remains true, the loop never ends. A loop that has no end point is known as infinite loop.

In Python the body of the loop is determined by indentations. The non zero values are considered to be true by Python interpreter and None or 0 are considered to be false by Python interpreter.

 

Flowchart of while loop

flowchart-of-while-loop

EXAMPLE of Python while loop

Consider the following example in which we are using the while loop to add the whole numbers up to the number that will be entered by the user:

CODE

>>> n = int(input(“Enter a number: “))

Enter a number: 5

>>> sum = 0

>>> j = 0

>>> while j<=n:

          sum = sum + j

          j = j+1

>>> print (“The sum of natural numbers is: “, sum)

OUTPUT

The sum of natural numbers is:  15

the-sum-of-natural-numbers-is-15

In the above example, a number is entered by the user, the counter variable of while loop is initialized at 0 because we are finding the sum of whole numbers the loop will be executed till j becomes smaller than and equal to n and n is 5 (entered by user). Inside the body of the loop, we are adding j to sum and storing the resulting value in the variable sum and j is incremented by 1. At last outside the body of the loop the variable sum is printed that had the value of 15.

It can be seen in the above example that the counter variable is incremented inside the body of the loop this is very important because if the variable is not incremented then our loop will become an infinite loop.

 

while loop with else

In Python we can use the else part with the ‘while loop’ as we did in the ‘for loop’, this is completely optional. The else part of the loop will be executed when the condition becomes false. It means that when the point of termination of the loop becomes the else part will be executed.

In Python or any other programming language we can use the break statement also which will help to stop the loop. When the break statement is used the else part of the ‘while loop’ will be ignored that is the else part will not be executed. The else part of the ‘while loop’ will run if the condition of while loop becomes false or there is no break statement.

EXAMPLE

Consider the following example in which we have used while loop with else and printed a message. In the following example the else part and the loop part both will be executed:

CODE

>>> i = 0

>>> while i < 4:

          print(“The control is inside the loop”)

          i = i+1

else:

          print(“The control is inside the else part”)

OUTPUT

The control is inside the loop

The control is inside the loop

The control is inside the loop

The control is inside the loop

The control is inside the else part

the-control-is-inside-the-else-part

In the above example, the loop is executed four times as the counter variable was equal to 0 and the condition was that when i becomes smaller than 4 the loop should be terminated. When the loop was terminated the else part of the loop executed and the message was printed accordingly that the control is now outside the loop. It can be seen in the above example that the counter variable is incremented so as to avoid infinite loop.