While


The structure of a while statement is:

while <condition>:
    <do stuff>

The program answers the question: is the condition true or false? If it is true, it does stuff in the <do stuff> section.  When it is finished it goes back up to the top and repeats the process again and again until the condition is false:

>>> a = 10
>>> while a > 0:
...    a = a-1
...    print a
...
9
8
7
6
5
4
3
2
1
0

The ordering here is a little odd because I forgot to print a in the first line.  What is printed is the value of a after 1 has been subtracted from it. Note now that the value stored in  a has been changed while the program did that stuff.  It is now 0 (zero, not that letter in the alphabet after n).

>>> a
0

Anything that can be put in the <condition> part of an if statement can also go in while‘s <condition> statement.

>>> while True:
...    b = raw_input('Type stuff here: ')
...    if b == 'quit':
...         break
...    print 'You typed -> '+b
...
Type stuff here: hi
You typed -> hi
Type stuff here: what's this do?
You typed -> what's this do?
Type stuff here: hello? Is someone there?
You typed -> hello? Is someone there?
Type stuff here: quit
>>>

In this example, we’ve used a weird condition- True.  The computer, when asked the question, “Is True true or is it false?” will have to answer “True is true!”.  It will do this every time the condition is encountered.  Therefore this is a loop which would go on for ever.  Or, it would go on forever if we hadn’t used another instruction break.  What break does, is it ‘breaks’ out of the loop created by while and continues its execution at the next line following the <do stuff> block.  You should be able to see this because it didn’t print out “You typed -> quit” when I typed quit.  That code was skipped.  Break will also work to break out of a loop for the for command.

>>> for a in range(20):
...          print a
...          if a == 10:
...               break
...
0
1
2
3
4
5
6
7
8
9
10

Do you see it broke out of the loop when a was 10?

You can give set up an initial condition before starting the while loop and update it within the loop:

>>> a = 5
>>> while a < 100:
...    print a
...    a = a+10
...
5
15
25
35
45
55
65
75
85
95

But you need to be careful that the loop doesn’t go on forever!  If the program is not responsive try Ctrl-C to stop it:

>>> while True:
...     pass
...

^CTraceback (most recent call last):
 File "<stdin>", line 1, in <module>
KeyboardInterrupt

6 Responses to While

  1. When writing for kids please be careful to note both the version of
    python you are using and the reasons for not using
    later version such as 3.1.2 which is recommended
    for new projects. I am (sob,sob) trying to work cross
    platform between Windows Vista 64 bit, Mac OS X
    10.4, & various Ubuntu’s using 3.1.2. It is a real
    pain to read through a site such as PyGt only to
    realize after two days it doesn’t work with 64 bit
    python 3.1.2, only 32 bit. Not all
    things work across all platforms or work differently. The Vista dos shell can drive a new person
    nuth with its full path, double backslashes not to mention cut and paste oddities.

  2. Curly says:

    “The Vista dos shell can drive a new person nuth”

    A new person should be using Idle or another IDE IMHO. It also avoids the necessary “Press Enter” statement at the end of a program so you can see what has been printed. It is nice to see someone explaining a while() loop to beginners. For some reason programmers don’t like using while statements. I don’t know why.

    “Anything that can be put in the part of an if statement can also go in while‘s statement.”

    A picky point is that a for() statement is a special form of a while() statement so technically it should be the other way around.

  3. Pingback: Consolidation: CryptoPyThy « Python Tutorials for Kids 8+

  4. lokeyokeyee says:

    in a way

  5. yi heng says:

    easy as easy

Leave a reply to Curly Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.