Friday, 20 July 2012

The Infinite Loops


You must use caution when using while loops because of the possibility that this condition never resolves to a false value. This results in a loop that never ends. Such a loop is called an infinite loop.
An infinite loop might be useful in client/server programming where the server needs to run continuously so  that client programs can communicate with it as and when required.


Example:

Following loop will continue till you enter CTRL + C

#!/usr/bin/python

var = 1
while var == 1 :  #This construct an infinite loop
     num = raw_input (“Enter a number  :”)
    print “You entered: ”, num
print “Good bye!”

This will produce following result:

Enter a number  :20
You entered: 20
Enter a number  :29
You entered: 29
Enter a number  :3
You entered: 3
Enter a number between : Trackback  (most recent call last):
   File “text.py”, line 5, in <module>
    num = raw_input(“Enter a number : ”)
keyboardInterrupt

No comments:

Post a Comment