Control-Loops

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值