Python -  while loop

A Python ‘while loop’ repeatedly carries out a target statement while the condition is true.

The loop iterates as long as the defined condition is true. When it ceases to be true and becomes false, control passes to the first line after the loop.

Python while statement is the most general iteration construct in the language.

Its called a loop because control keeps looping back to the start of the statement until the test becomes false.

Syntax

while expression:
   statement(s)

 

Flow Diagram

 

while loop in Python

 

Example


a = 0; b=10
while (a < b):
   print(a, end=' ')
   a = a+1

Output

 0 1 2 3 4 5 6 7 8 9