while_statement

=while Loop Statement= A loop is a construct that causes a section of a program to be repeated a certain number of times. The repetition continues while the condition set for the loop remains //true//. When the condition becomes false, the loop ends and the program control is passed to the statement following the loop. This tutorial will discuss the //while// loop construct available in Python. The //while// Loop: 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: code while expression: statement(s) code || 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. **Note:** In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. Example: code #!/usr/bin/python

count = 0 while (count < 9): print 'The count is:', count count = count + 1

print "Good bye!" code || This will produce following result: code 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! code || The block here, consisting of the //print// and increment statements, is executed repeatedly until count is no longer less than 9. With each iteration, the current value of the index count is displayed and then increased by 1. 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 : code #!/usr/bin/python

var = 1 while var == 1 : # This constructs an infinite loop num = raw_input("Enter a number :") print "You entered: ", num

print "Good bye!" code || This will produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">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 :Traceback (most recent call last): File "test.py", line 5, in    num = raw_input("Enter a number :") KeyboardInterrupt code || <span style="background-color: #ffffff; display: block; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Above example will go in an infite loop and you would need to use CTRL+C to come out of the program. Single Statement Suites: <span style="background-color: #ffffff; display: block; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Similar to the **if** statement syntax, if your **while** clause consists only of a single statement, it may be placed on the same line as the while header. <span style="background-color: #ffffff; display: block; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here is the syntax of a one-line while clause: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">while expression : statement code ||