Python while loop

Loops are used for the repeated execution of a code until the desired condition is met. In this tutorial, you will learn about Python while loop.

While loop: Itroduction
While loop: Syntax
While loop: Flowchart
The infinite while loop
While loop example
If statement and while loop

python while loop

In Python, there are two types of loops only.

Python programming while loop


Loops are either infinite or conditional. Python while loop keeps reiterating a block of code defined inside it until the desired condition is met.

The while loop contains a boolean expression and the code inside the loop is repeatedly executed as long as the boolean expression is true.

Python while loop – Syntax

while(expression)
     statement(s)

As seen in the syntax, while loop runs till the boolean expression returns TRUE. The statements that are executed inside while can be a single line of code or a block of multiple statements.

Flowchart of Python while loop

python while loop flowchart

One key thing to be noted is that the while loop is entry controlled, which means the loop can never run and the while loop is skipped if the initial test returns FALSE.

For example, following code inside the while loop will be never executed because the initial test will return FALSE.

i = 5
while (i > 8):
  print ('This is while loop')
  i++

Here in this program while loop won’t be executed because in initial test i > 8 will return FALSE as the value of i is 5.

 [adsense1]

The infinite while loop in Python

While the loop is skipped if the initial test returns FALSE, it is also forever repeated infinitely if the expression always returns TRUE.

For example, while loop in the following code will never exit out of the loop and the while loop will iterate forever.

i = 5
while (i = 5):
  print ('Infinite loop')

In this example, the value of i will always be 5, so the expression will always return TRUE resulting the iteration of while loop infinitely. The program will never stop and will continue printing ‘infinite loop’ forever.

Python while loop: Example

#program to display 1 to 9
i = 1
while (i < 10):
  print (i)
  i = i+1

Output

This program will initially check if the value of i is less than 10 or not. If it is TRUE, then it will print the value of i and the value of i will be increased by 1. This process will be repeated until the value of i is less than 10 i.e. 9.

Hence, it will generate following output.

1
2
2
4
5
6
7
8
9

Python: Using If statement with while loop


Till now we discussed iterating a block of code in while loop until a condition is met.

What if we want to impose another condition inside while loop and break out of while loop even without meeting condition in while loop expression?

For this, we can use if else statement to check a condition and break keyword to jump out of while loop even without completing the expression in while loop.

Here is the example to illustrate this.

a = 1
b = 1
while (a<10):
  print ('Iteration',a)
  a = a + 1
  b = b + 1
  if (b == 4):
    break
print ('While loop terminated')

Output

This script will produce following output.

Iteration 1
Iteration 2
Iteration 3
While loop terminated

Explanation

In above program, while loop is supposed to iterate 9 times until the value of a is less than 10.

In the first three iterations Iteration 1, Iteration 2 and Iteration 3 is printed in the console.

Until this point the value of a and b both is 3, hence the if statement is not executed. After the 3rd iteration the value of a and b both becomes 4 and the expression in if statement return TRUE triggering the break statement as the value of b is equal to 4.

Hence, the flow of program jumps out of the loop before completing 9 iterations and while loop terminated is printed out in the console.

This is all about the Python while loop. In the next tutorial, you will learn about Python for loop.