McConnell_2004_CH16_ControllingLoops Flashcards
- What is one typical novice programmer misconception about the while loop?
Novice 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.
- Why do some recommend using only one kind of control structure (loop with test at beginning)?
idk but
(For a loop that tests at the beginning, you can use a while loop in C++, C#, Java, Visual Basic, and most other languages.
- How can an ‘endless’ while loop be created in JADE
endless loop?
- How can a while loop be exited from within?
Many languages provide a means of causing a loop to terminate in some way other than completing the for or while condition. The break statement (or equivalent) causes a loop to terminate through the normal exit channel; the program resumes execution at the first statement following the loop.
- When should a foreach loop be used in preference to a while loop?
The 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.
- When should a foreach loop be used in preference to a while loop?
The 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.
- Where should loop initialisation code be put?
Put initialization code directly before the loop. The Principle of Proximity advocates putting related statements together. If related statements are strewn across a routine, it’s easy to overlook them during modification and to make the modifications incorrectly. If related statements are kept together, it’s easier to avoid errors during modification. Keep loop-initialization statements with the loop they’re related to. If you don’t, you’re more likely to cause errors when you generalize the loop into a bigger loop and forget to modify the initialization code. The same kind of error can occur when you move or copy the loop code into a different routine without moving or copying its initialization code. Putting initializations away from the loop—in the data-declaration section or in a housekeeping section at the top of the routine that contains the loop—invites initialization troubles.
- Where should loop initialisation code be put?
Put initialization code directly before the loop. The Principle of Proximity advocates putting related statements together. If related statements are strewn across a routine, it’s easy to overlook them during modification and to make the modifications incorrectly. If related statements are kept together, it’s easier to avoid errors during modification. Keep loop-initialization statements with the loop they’re related to. If you don’t, you’re more likely to cause errors when you generalize the loop into a bigger loop and forget to modify the initialization code. The same kind of error can occur when you move or copy the loop code into a different routine without moving or copying its initialization code. Putting initializations away from the loop—in the data-declaration section or in a housekeeping section at the top of the routine that contains the loop—invites initialization troubles.
- How long should a loop be?
Loop length can be measured in lines of code or depth of nesting. Here are some guidelines:
Make your loops short enough to view all at once If you usually look at loops on 66-line paper, that puts a 66-line restriction on you. If your monitor displays 50 lines, that puts a 50-line restriction on you. Experts have suggested a loop-length limit of one printed page, or 66 lines. When you begin to appreciate the principle of writing simple code, however ,you’ll rarely write loops longer than 15 or 20 lines.
- Outline a general process or design approach for creating looping code?
Here’s the general process. Start with one case. Code that case with literals. Then indent it, put a loop around it, and replace the literals with loop indexes or computed expressions. Put another loop around that, if necessary, and replace more literals. Continue the process as long as you have to. When you finish, add all the necessary initializations. Since you start at the simple case and work outward to generalize it, you might think of this as coding from the inside out.