Python While Loop

A while loop statement in Python prog ramming lang uag e repeatedly executes a targ et statement as long as a g iven condition is true. Syntax: T he syntax of a while loop in Python prog ramming lang uag e is: while expression: statement(s) Here, statement(s) may be a sing le statement or a block of statements. T he c ondition may be any expression, and true is any non-zero value. T he loop iterates while the condition is true. When the condition becomes false, prog ram control passes to the line immediately following the loop. In Python, all the statements indented by the same number of character spaces after a prog ramming construct are considered to be part of a sing le block of code. Python uses indentation as its method of g rouping statements. Flow Diag ram: Here, key point of the while loop is that the loop mig ht not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed. Example: #!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" When the above code is executed, it produces the following result: The count The count The count The count The count The count The count The count The count Good bye! is: is: is: is: is: is: is: is: is: 0 1 2 3 4 5 6 7 8 T he block here, consisting of the print and increment statements, is executed repeatedly until count is no long er less than 9. With each iteration, the current value of the index count is displayed and then increased by 1. The Infinite Loop: A loop becomes infinite loop if a condition never becomes false. You must use caution when using while loops because of the possibility that this condition never resolves to a false value. T his results in a loop that never ends. Such a loop is called an infinite loop. An infinite loop mig ht be useful in client/server prog ramming where the server needs to run continuously so that client prog rams can communicate with it as and when required. #!/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!" When the above code is executed, it produces the 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 :Traceback (most recent call last): File "test.py", line 5, in num = raw_input("Enter a number :") KeyboardInterrupt Above example will g o in an infite loop and you would need to use CT RL+C to come out of the prog ram. The else Statement Used with Loops Python supports to have an else statement associated with a loop statement. If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. If the else statement is used with a while loop, the else statement is executed when the condition becomes false. T he following example illustrates the combination of an else statement with a while statement that prints a number as long as it is less than 5, otherwise else statement g ets executed. #!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" When the above code is executed, it produces the following result: 0 1 2 3 4 5 is is is is is is less than 5 less than 5 less than 5 less than 5 less than 5 not less than 5 Sing le Statement Suites: Similar to the if statement syntax, if your while clause consists only of a sing le statement, it may be placed on the same line as the while header. Here is the syntax and example of a one-line while clause: #!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!" Its better not try above example because it will g o into infinite loop and you will have to use CT RL+C keys to come out.

Comments