Python: while loop


The while loop is used to execute a piece of code as long as the test condition is true. While loop is preferred whenever the number of iterations is not known.

Syntax

Python while loop

Example

	num=5
	count=1
	while count <= num:
	    print("The current number is:",count)
	    count+=1

Output

The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5

In the above example, while loop will check the condition continuously the value count and compare with num value as long as condition is true. In case of true all the statement inside the while loop will execute.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s