8.5            whileStatement

while声明
 

Python’s whileis the first looping statementwe will look at in this chapter. In fact, it is a conditional loopingstatement. In comparison with an  ifstate- ment where a true expression will result in a single execution of the ifclause suite, the suite in a whileclause will be executed continuously in a loop until that condition is no longer satisfied.

Python的while是本章第一个介绍的循环声明,事实上是一个条件循环声明。与if声明相比,如果if表达式为真,就会执行一次相应的代码段;while中的代码段在循环中一直执行,直到循环条件不再为真。
 
 
 

 

8.5.1                                                                                   General Syntax

 

通用语法

 
 

Here is the syntax for a whileloop:

 
while expression:
suite_to_repeat
 
 
 
 
The suite_to_repeatclause of the whileloop will be executed continu-
ously in a loop until expressionevaluates to Boolean False. This type of

looping mechanism is often used in a counting situation, such as the example

in the next subsection.

      下面是while循环的语法:
while expression:
suite_to_repeat
while循环的suite_to_repeat语句会一直在循环中执行,直到expression值为布尔假。
这种类型的循环机制常常用在技术情况下,如下面接下来的例子。
 
 

 

8.5.2                Counting Loops

 

计数循环

 
count = 0
while (count < 9):
print 'the index is:', count count += 1
 

Thesuite here, consisting of the printandincrement statements, is exe- cuted repeatedly until countis no longer less than 9. With each iteration, the current value of the index countisdisplayed and then bumped up by

1. If we take this snippet of code to the Python interpreter, entering the source and seeing the resulting execution would look something like:
 
>>> count = 0
>>> while (count < 9):

...     print 'the index is:', count

...     count
+=
1
...
 
 
the index is:
0
 
the index is:
1
 
the index is:
2
 
the index is:
3
 
the index is:
4
 
the index is:
5
 
the index is:
6
 
the index is:
7
 
the index is:
8
 
 
 

 

8.5.3                Infinite Loops

 

无穷循环

 

One  must  use  caution  when  using  whileloops  because  of  the  possibility that the condition never resolves to a false value. In such cases, we would have a loop that never ends on our hands. These “infinite” loops are not nec- essarily bad things—many communications “servers” that are part of client/ server  systems  work  exactly  in  that  fashion.  It  all  depends  on  whether  or not the loop was meant to run forever, and if not, whether the loop has the possibility of terminating; in other words, will the expression ever be able to evaluate to false?

 
while True:

handle, indata = wait_for_client_connect() outdata = process_request(indata) ack_result_to_client(handle, outdata)

 

For example, the piece of code above was set deliberately to never end because Trueis not going to somehow change to False. The main point of this server code is to sit and wait for clients to connect,presumably over a network link. These clients send requests which the server understandsand processes.

After the request has been serviced, a return value or data is returned to the  client  who  may  either  drop  the  connection  altogether  or  send  another request. As far as the server is concerned, it has performed its duty to this one client and returns to the top of the loop to wait for the next client to come along. You will find out more about client/server computing in Chapter 16, “Network Programming” and Chapter 17, “Internet  Client Programming.”