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 suite is executed continuously in a loop until the expression becomes zero or false; execution then continues on the first succeeding statement.

>>> counter = 0
>>> while counter < 5:
…       print 'loop #%d' % (counter)
…       counter = counter + 1

loop #0
loop #1
loop #2
loop #3
loop #4
loop #5

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

Get Core Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.