循环

最后更新于:2022-04-02 04:17:00

[TOC] ## 将判断外提 bad ``` for (i=0;i 此代码虽然提升了性能,但是违背了可读性,所以视情况而定 ## 合并循环 bad ``` VB 示例 For i=0 to employCount -1 employeeName(i)="" Next For i=0 to employCount -1 employeeEarnings(i)=0 Next ``` good ``` For i=0 to employCount -1 employeeName(i)="" employeeEarnings(i)=0 Next ``` ## 尽可能减少在循环内部做的工作 bad ``` // C++ 示例 for ( i=0;idiscounts->factors->net } ``` good ``` // C++ 示例 quantityDiscount = rates->discounts->factors->net; for ( i=0;i 0) else -1 ``` ## 示例2 bad 每次循环都要进行好几次判断 ``` found = FALSE; i = 0; while( (!found) && (i ';