While Repetition Structure

A repetition structure allows the programmer to specify that a program should repeat an action while some condition remains true. The pseudocode statement

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

Python 2.2b2

(#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on


Type "help",

"copyright", "credits" or "license" for more informa-


>>> if 1 < 2

... pass

Fig. 3.7 Keyword pass.

80 Control Structures describes the repetition that occurs during a shopping trip. The condition, "there are more items on my shopping list" is either true or false. If it is true, the program performs the action "Purchase next item and cross it off my list." This action is performed repeatedly while the condition remains true.

The statement(s) contained in the while repetition structure constitute the body (suite) of the while. The while structure body can consist of a single statement or multiple statements. Eventually, the condition should evaluate to false (in the above example, when the last item on the shopping list has been purchased and crossed off the list). At this point, the repetition terminates, and the program executes the first statement after the repetition structure.

Common Programming Error 3.6

A logic error, called an infinite loop (the repetition structure never terminates), occurs when an action that causes the condition in the while structure to become false is missing from the body of a while structure.

Common Programming Error 3.7

Spelling the keyword while with an uppercase W, as in While (remember that Python is a case-sensitive language), is a syntax error. All of Python's reserved keywords, such as while, if, elif and else, contain only lowercase letters.

As an example of a while structure, consider a program segment designed to find the first power of 2 larger than 1000. Suppose variable product has been created and initialized to 2. When the following while repetition structure finishes executing, product will contain the desired answer:

product = 2

while product <= 1000: product = 2 * product

At the start of the while structure, product is 2. The variable product is multiplied by 2, successively taking on the values 4, 8, 16, 32, 64, 128, 256, 512 and 1024. When the value of product equals 1024, the while structure condition, product <= 1000, evaluates to false. This terminates the repetition—the final value of product is 1024. Program execution continues with the next statement after the while structure.

The flowchart of Fig. 3.8 illustrates the flow of control in the while structure that corresponds to the preceding while structure. Once again, note that (besides small circles and arrows) the flowchart contains a rectangle symbol and a diamond symbol.

Fig. 3.8 while repetition structure flowchart.

Chapter 3

Imagine a bin of empty while structures that can be stacked and nested with other control structures to implement an algorithm's flow of control. The empty rectangles and diamonds are then filled in with appropriate actions and decisions. The flowchart shows the repetition. The flowline emerging from the rectangle wraps back to the decision that is tested each time through the loop until the decision becomes false. Then, the while structure exits and control passes to the next statement in the program.

0 0

Post a comment