Knowledge Base‎ > ‎Programming‎ > ‎Python‎ > ‎Python Reference‎ > ‎

while


Related:

 Examples

 
 
n = 0
while n < 10:
  print n
  n = n + 1

 Description        

 
The while statement repeats the code directly below it (and indented) until it's condition is no longer true.

The loop can also be ended by using the break command.

 Syntax

 
 
while condition:
    statement(s)

 Parameters

 
condition - an expression that evaluates to true or false

statement(s) - statements to execute as part of the loop

 Interpreter

 Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> n = 0

>>> while n < 3:

...   print n

...   n = n + 1

...

0

1

2

>>>





Comments