Python while loop

The while loop is useful to execute a group of statements several times repeatedly depending on whether a condition is True or False.

while loop

The syntax or format of while loop is:

CopiedCopy Code

while condition: 
	statements
						

Here, 'statements' represents one statement or a suite of statements.

Python interpreter first checks the condition. If the condition is True, then it will execute the statements written after colon (:). After executing the statements, it will go back and check the condition again

If the condition is again found to be True, then it will again execute the statements. Then it will go back to check the condition once again. In this way, as long as the condition is True, Python interpreter executes the statements again and again.

Once the condition is found to be False, then it will come out of the while loop.


Flowchart of while Loop

Python while loop
CopiedCopy Code

x=1 
while x<=10: 
  print(x) 
  x+=1 
  print("End")