Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

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.

  • + Share This
  • 🔖 Save To Your Account