A repetition structure allows the programmer to specify that an action is to be repeated
while a condition remains true. The pseudocode statement
While there are more items on my shopping list
Purchase next item and cross it off my list
describes the repetition that occurs during a shopping trip. The condition, “there are more items on my shopping list” may be true or false. If it is true, then the action, “Purchase next item and cross it off my list” is performed. This action executes repeatedly while the condition remains true. The statement(s) contained in the while repetition structure constitute the body of the while. The while structure body may be a single statement or a block. Eventually, the condition becomes false (when the last item on the shopping list has been purchased and crossed off the list). At this point, the repetition terminates, and the first statement after the repetition structure executes.
As an example of a while structure, consider a program segment designed to find the
first power of 2 larger than 1000. Suppose int variable product contains the value 2.
When the following while structure finishes executing, product contains the result:
int product = 2;
while ( product <= 1000 )
product = 2 * product;
The flowchart in Fig. 4.5 illustrates the flow of control of the preceding while repetition structure. Once again, note that (besides small circles and arrows) the flowchart contains only a rectangle symbol and a diamond symbol.
Common Programming Error 4.3
Not providing in the body of a while structure an action that eventually causes the condition to become false is a logic error. Normally, such a repetition structure will never terminate, which is an error called an “infinite loop.”
Common Programming Error 4.4
Beginning the keyword while with an uppercase W, as in While, is a syntax error. Remember that C# is a case-sensitive language. All of C#’s keywords—while, if, else, etc.contain only lowercase letters.
Testing and Debugging Tip 4.2
Visual Studio .NET will not color a keyword properly unless that keyword is spelled correctly and with the correct case.
Imagine, again, a deep bin of empty while structures that may be stacked and nested with other control structures to form a structured implementation of an algorithm’s flow of control. The empty rectangles and diamonds are filled with appropriate actions and decisions. The flowchart clearly shows the repetition. The flowline emerging from the rectangle indicates that program control continues with the decision, which is tested during each iteration of the loop until the decision eventually becomes false. At this point, the while structure terminates, and control passes to the next statement following the while structure in the program.
When the while structure begins executing, product is 2. Variable product is
repeatedly multiplied by 2, taking on the values 4, 8, 16, 32, 64, 128, 256, 512 and 1024,successively. When product becomes 1024, the condition product <= 1000 in the while structure becomes false. This terminates the repetition with 1024 as product’s final value. Execution continues with the next statement after the while. [Note: If a while structure’s condition is initially false, the body statement(s) will never be executed.]
0 comments:
Post a Comment