3.7 while Statement

The while statement allows you to repeat one or more actions while a condition remains True. Such a statement often is called a loop.

The following pseudocode specifies what happens when you go shopping:

While there are more items on my shopping list
        Buy next item and cross it off my list

If the condition “there are more items on my shopping list” is true, you perform the action “Buy next item and cross it off my list.” You repeat this action while the condition remains true. You stop repeating this action when the condition becomes false—that is, when you’ve crossed all items off your shopping list.

Let’s use a while statement to find the first power of 3 larger than 50:

In [1]: product = 3

In [2]: while product <= 50:
   ...:     product = product * 3
   ...:

In [3]: product
Out[3]: 81

First, we create product and initialize it to 3. Then the while statement executes as follows:

  1. Python tests the condition product <= 50, which is True because product is 3. The statement in the suite multiplies product by 3 and assigns the result (9) to product. One iteration of the loop is now complete.

  2. Python again tests the condition, which is True because product is now 9. The suite’s statement sets product to 27, completing the second iteration of the loop.

  3. Python again tests the condition, which is True because product is now 27. The suite’s statement sets product to 81, completing the third iteration of the loop.

  4. Python again tests the condition, which is finally False because product is now 81. The repetition now terminates.

Snippet [3] evaluates product to see its value, 81, which is the first power of 3 larger than 50. If this while statement were part of a larger script, execution would continue with the next statement in sequence after the while.

Something in the while statement’s suite must change product’s value, so the condition eventually becomes False. Otherwise, a logic error called an infinite loop occurs. Such an error prevents the while statement from ever terminating—the program appears to “hang.” In applications executed from a Terminal, Command Prompt or shell, type Ctrl + c or control + c (depending on your keyboard) to terminate an infinite loop. IDEs typically have a toolbar button or menu option for stopping a program’s execution.

while Statement Flowchart

The following flowchart shows the preceding while statement’s flow of control:

An image of a partial flowchart that shows a while statement’s flow of control with a true and false result.

Follow the flowlines to experience the repetition. The flowline from the rectangle “closes the loop” by flowing back into the condition product <= 50 that’s tested during each iteration. When that condition becomes False, the while statement exits and control proceeds to the next statement in sequence.

tick mark Self Check

  1. (True/False) A while statement performs its suite while some condition remains True.
    Answer: True.

  2. (IPython Session) Write statements to determine the first power of 7 greater than 1000.
    Answer:

    In [1]: product = 7
    
    In [2]: while product <= 1000:
       ...:     product = product * 7
       ...:
    
    In [3]: product
    Out[3]: 2401
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.196.217