Friday, 20 July 2012

while Loop of python


The while loop is one of the looping constructs available in Python. The while loop continues until the expression becomes false. The expression has to be a logical expression and must return either a true or a false value.

The syntax of the while loop is :

While expression:
     Statement(s)

Here expression statement is evaluated first. If expression is true that is, then the statement(s) block is executed repeatedly until expression becomes false. Otherwise, the next statement following the statement(s) block is executed.

Example:

#!/usr/bin/python

count = 0
while (count < 9)
            print ‘The count is :’ , count
            count = count + 1
print “Good bye!”

This will produce following result:

The count is : 0
The count is : 1
The count is : 2
The count is : 3
The count is : 4
The count is : 5
The count is : 6
The count is : 7
The count is : 8
Good bye!

No comments:

Post a Comment