Python While Loop

Python While Loop is a condition-based loop that repeatedly executes the associated statements until the loop is true. unlike Python for loop, while loop works with the associated condition.
Syntax
The syntax of a while loop in python language is as follows-

while condition:
      statement1
else:
      statement2

NOTE: else statement in while loop is optional and executes when the condition results in false.
Here, statement1, statement2 may be a single statement or a block statement. The condition may be any expression, A non Zero value is true. The loop iterates until the condition is true.

When the condition results in false, program control moves to else statement if used otherwise move immediately following the loop statements.

In Python, connected statements are indented an equal number of spaces after the programming construct otherwise they will are not treated as a connected statement and behave as an independent statement.

Flow diagram of while loop

Python-while-Loop-Syntax

NOTE: The loop will not execute even once if the associated condition results in false.

Example

count =0
while count<10:
     count= count+1
     print(' Count is :', count)

print(' Good to see you here')
When the above code is executed, it produces the following result
count is : 1
count is : 2
count is : 3
count is : 4
count is : 5
count is : 6
count is : 7
count is : 8
count is : 9
count is : 10
Good to see you here

The code block here consists of a while loop and a print and increment statement is executed repeatedly until the count is no longer less than 9. With each iteration count is incremented and displayed on the screen.

infinite Loop

A loop becomes an infinite loop if the associated condition never becomes FALSE. Utmost care must be taken when using a while loop because of the possibility of a condition that never results in FALSE. This results in a loop that never ends, such loops are called infinite loops.

Example

count =0
while count>=0:            # This expression will always result in true- Thus infinite loop
      count= count+1
      print('count is :',count

When the above code is executed, will produce the following result

count is : 1
count is : 2
count is : 3
count is : 4
count is : 5
count is : 6
count is : 7
count is : 8
count is : 9
count is : 10
count is : 11
.....
......
......
count is : 128
count is : 129
count is : 130
count is : 131
count is : 132
count is : 133
count is : 134
Traceback (most recent call last):
  File "C:/Users/rakesh/Desktop/infinite_loop.py", line 4, in 
    print('count is :',count)
KeyboardInterrupt

The above example goes as an infinite loop, Use CTRL+C to exit the program execution.

Using else statement with while loop

The else statement used with while loop is executed when the condition becomes false.

The following example shows the working of the else statement in while loop-

example

count=0
while count<5:
      count =count+1
      print(' count is :', count)
else:
     print(' Value of count is not less than 5')

When the above code is executed, it produces the following result

 count is : 1
 count is : 2
 count is : 3
 count is : 4
 count is : 5
 Value of count is not less than 5

Hope these examples will help to understand the working of while loop in Python. While loop exercise with the solution is recommended reading after this.

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.