Section 2.12. while Loop


2.12. while Loop

The standard while conditional loop statement is similar to the if. Again, as with every code sub-block, indentation (and dedentation) are used to delimit blocks of code as well as to indicate which block of code statements belong to:

while expression:     while_suite


The statement while_suite is executed continuously in a loop until the expression becomes zero or false; execution then continues on the first succeeding statement. Like if statements, parentheses are not required with Python while statements.

   >>> counter = 0    >>> while counter < 3:    ...     print 'loop #%d' % (counter)    ...     counter += 1    loop #0    loop #1    loop #2


Loops such as while and for (see below) are covered in the loops section of Chapter 8.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net