CSDN博客

img wangjiong

Control-Loops

发表于2004/10/10 23:36:00  772人阅读

分类: Excerpt of CC2

Entering the Loop
1. Enter the loop from one location only.
2. Put initialize code directly before the loop.
3. In C++ and Java, use for(;;) or while(true) for infinite loops.
4. Don't use a for loop when a while loop is more appropriate.

Processing the middle of the loop
1. Use { and } to enclose the statements in a loop.
2. Avoid empty loops.
3. Keep loop-hosekeeping chores at either the beginning or the end of the loop.
4. Make each loop perform only one function.

Exiting the loop
1. Assure yourself that the loop ends.
2. Make loop-termination conditions obvious.
3. Don't monkey with the loop index of a for loop to make the loop terminate.
4. Avoid code that depends on the loop index's final value.
5. Consider using safety counters

Exiting loops early
1. Consider using break statements rather than boolean flags in a while loop.
2. Be wary of a loop with a lot of breaks scattered through it.
3. Use continue for tests at the top of a loop.
4. Use labeled break if your language supports it.
5. Use break and continue only with caution.

Using loop variables.
1. Use ordinal or enumerated types for limits on both arrays and loops.
2. Use meaningful variable names to make nested loops readable.
3. Use meaningful names to avoid loop-index cross talk.
4. Limit the scope of loop-index variables to the loop itself.

How long should a loop be
1. Make your loops short enough to view all at once.
2. Limit nesting to three levels.
3. Move loop innards of long loops into routines.
4. Make long looops especially clear.

 

0 0

相关博文

我的热门文章

img
取 消
img