< Free Open Study > |
16.1. Selecting the Kind of LoopIn most languages, you'll use a few kinds of loops:
The kinds of loops are differentiated first by flexibility�whether the loop executes a specified number of times or whether it tests for completion on each iteration. The kinds of loops are also differentiated by the location of the test for completion. You can put the test at the beginning, the middle, or the end of the loop. This characteristic tells you whether the loop executes at least once. If the loop is tested at the beginning, its body isn't necessarily executed. If the loop is tested at the end, its body is executed at least once. If the loop is tested in the middle, the part of the loop that precedes the test is executed at least once, but the part of the loop that follows the test isn't necessarily executed at all. Flexibility and the location of the test determine the kind of loop to choose as a control structure. Table 16-1 shows the kinds of loops in several languages and describes each loop's flexibility and test location.
When to Use a while LoopNovice programmers sometimes think that a while loop is continuously evaluated and that it terminates the instant the while condition becomes false, regardless of which statement in the loop is being executed (Curtis et al. 1986). Although it's not quite that flexible, a while loop is a flexible loop choice. If you don't know ahead of time exactly how many times you'll want the loop to iterate, use a while loop. Contrary to what some novices think, the test for the loop exit is performed only once each time through the loop, and the main issue with respect to while loops is deciding whether to test at the beginning or the end of the loop. Loop with Test at the BeginningFor a loop that tests at the beginning, you can use a while loop in C++, C#, Java, Visual Basic, and most other languages. You can emulate a while loop in other languages. Loop with Test at the EndYou might occasionally have a situation in which you want a flexible loop, but the loop needs to execute at least one time. In such a case, you can use a while loop that is tested at its end. You can use do-while in C++, C#, and Java, Do-Loop-While in Visual Basic, or you can emulate end-tested loops in other languages. When to Use a Loop-With-Exit LoopA loop-with-exit loop is a loop in which the exit condition appears in the middle of the loop rather than at the beginning or at the end. The loop-with-exit loop is available explicitly in Visual Basic, and you can emulate it with the structured constructs while and break in C++, C, and Java or with gotos in other languages. Normal Loop-With-Exit LoopsA loop-with-exit loop usually consists of the loop beginning, the loop body (including an exit condition), and the loop end, as in this Visual Basic example: Visual Basic Example of a Generic Loop-With-Exit Loop
The typical use of a loop-with-exit loop is for the case in which testing at the beginning or at the end of the loop requires coding a loop-and-a-half. Here's a C++ example of a case that warrants a loop-with-exit loop but doesn't use one: C++ Example of Duplicated Code That Will Break Down Under Maintenance
The two lines of code at the top of the example are repeated in the last two lines of code of the while loop. During modification, you can easily forget to keep the two sets of lines parallel. Another programmer modifying the code probably won't even realize that the two sets of lines are supposed to be modified in parallel. Either way, the result will be errors arising from incomplete modifications. Here's how you can rewrite the code more clearly: C++ Example of a Loop-With-Exit Loop That's Easier to Maintain
Here's how the same code is written in Visual Basic: Visual Basic Example of a Loop-With-Exit Loop' Compute scores and ratings Consider these finer points when you use this kind of loop: Put all the exit conditions in one place. Spreading them around practically guarantees that one exit condition or another will be overlooked during debugging, modification, or testing. Cross-Reference Details on exit conditions are presented later in this chapter. For details on using comments with loops, see "Commenting Control Structures" in Section 32.5. Use comments for clarification. If you use the loop-with-exit loop technique in a language that doesn't support it directly, use comments to make what you're doing clear.
In common practice, the loop-with-exit loop isn't widely used yet. The jury is still locked in a smoky room arguing about whether it's a good practice for production code. Until the jury is in, the loop-with-exit loop is a good technique to have in your programmer's toolbox�as long as you use it carefully. Abnormal Loop-With-Exit LoopsAnother kind of loop-with-exit loop that's used to avoid a loop-and-a-half is shown here:
At first glance, this seems to be similar to the previous loop-with-exit examples. It's used in simulations in which // do something doesn't need to be executed at the first pass through the loop but // do something else does. It's a one-in, one-out control construct: the only way into the loop is through the goto at the top, and the only way out of the loop is through the while test. This approach has two problems: it uses a goto, and it's unusual enough to be confusing. In C++, you can accomplish the same effect without using a goto, as demonstrated in the following example. If the language you're using doesn't support a break command, you can emulate one with a goto. C++ Example of Code Rewritten Without a goto�Better Practice
When to Use a for LoopA for loop is a good choice when you need a loop that executes a specified number of times. You can use for in C++, C, Java, Visual Basic, and most other languages. Further Reading For more good guidelines on using for loops, see Writing Solid Code (Maguire 1993). Use for loops for simple activities that don't require internal loop controls. Use them when the loop control involves simple increments or simple decrements, such as iterating through the elements in a container. The point of a for loop is that you set it up at the top of the loop and then forget about it. You don't have to do anything inside the loop to control it. If you have a condition under which execution has to jump out of a loop, use a while loop instead. Likewise, don't explicitly change the index value of a for loop to force it to terminate. Use a while loop instead. The for loop is for simple uses. Most complicated looping tasks are better handled by a while loop. When to Use a foreach LoopThe foreach loop or its equivalent (foreach in C#, For-Each in Visual Basic, for-in in Python) is useful for performing an operation on each member of an array or other container. It has the advantage of eliminating loop-housekeeping arithmetic and therefore eliminating any chance of errors in the loop-housekeeping arithmetic. Here's an example of this kind of loop: C# Example of a foreach Loopint [] fibonacciSequence = new int [] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; |
< Free Open Study > |
No comments:
Post a Comment