循环用C - 为()或while() - 这是最好的? [英] Loops in C - for() or while() - which is BEST?
问题描述
有关()或while() - 这是最好的。
为(i = 1; I< A;我++)
/* 做一点事 */
或
I = 1;
而(ⅰ&下;一个){
/* 做一点事 */
我++;
}
首先是地道的表达方式;这是大多数C codeRS会期望看到的。不过,我要指出,大多数人也会希望看到
为(i = 0; I< A;我++)
注意循环从零开始。这将做一些事情 A
倍。如果你打算写一个,而
循环,相当于为
循环如上我强烈建议您它写为为
循环。再次,这是C codeRS期望看到的。此外,作为一个为
循环很容易因为一切阅读(初始化,循环条件,前pression每次迭代后执行)都是在一行上。对于,而
循环,他们有s $ P $垫了阻碍可读性。
请注意,但是,也有在哪些情况下看似相当于为
和,而
环路实际上不是。例如:
为(i = 0;我小于10;我++){
如果(我== 5)继续;
的printf(%d个\\ N,I);
}
和
I = 0;
而(ⅰ小于10){
如果(我== 5)继续;
的printf(%d个\\ N,I);
我++;
}
第一眼看上去是等同的,但事实并非如此。在为
循环将打印 0--9
跳过 5
控制台上的控制台,而,而
循环将打印 0--4
,然后进入无限循环。
现在,处理,你问的简单情况。怎么样更复杂的情况下,你没有问?嗯,这真的取决于但一个好的经验法则是:如果你要重复的东西的时候固定的pre-确定的数字,为
循环一般情况下最好。否则,使用,而
循环。这不是一个硬性和快速的规则,但它是一个很好的原则进行的大拇指。例如,你可以写
无符号整型伏;
无符号整型℃;
为(C = 0; v的V>> = 1)C + = V&放大器; 1;
但我想大多数C程序员会写为
无符号整型伏;
无符号整型℃;
C = 0;
而(V){C + = V&安培; 1; V>> = 1; }
或者,作为另一个例子,如果你要读,直到文件的末尾,那么你应该使用,而
循环。因此,
FILE * FP;
FP = FOPEN(路径,R);
而(与fgets(buf中,最大,FP)!= NULL){/ *东西* /}
而不是
FILE * FP;
为(FP =的fopen(路径,R);与fgets(buf中,最大,FP)= NULL;!){/ *东西* /}
现在,深入到宗教的领地,这就是为什么我preFER 而(1)
作为做了一个无限循环的正确方法为(;;)
。
希望有所帮助。
for() or while() - which is BEST?
for (i=1; i<a; i++)
/* do something */
OR
i=1;
while (i<a) {
/* do something */
i++;
}
The first is the idiomatic way; it is what most C coders will expect to see. However, I should note that most people will also expect to see
for(i = 0; i < a; i++)
Note that the loop starts at zero. This will do something a
times. If you're going to write a while
loop that is equivalent to a for
loop as above I strongly encourage you to write it as a for
loop. Again, it is what C coders expect to see. Further, as a for
loop it is easier to read as everything (initialization, loop condition, expression to be executed after each iteration) are all on one line. For the while
loop they are spread out hindering readability.
Note, however, there are circumstances in which seemingly equivalent for
and while
loops are actually not. For example:
for(i = 0; i < 10; i++) {
if(i == 5) continue;
printf("%d\n", i);
}
and
i = 0;
while(i < 10) {
if(i == 5) continue;
printf("%d\n", i);
i++;
}
appear at first glance to be equivalent but they are not. The for
loop will print 0--9
skipping 5
on the console whereas the while
loop will print 0--4
on the console and then enter an infinite loop.
Now, that handles the simple case that you asked about. What about the more complex cases that you didn't ask about? Well, it really depends but a good rule of thumb is this: if you are going to repeat something a fixed pre-determined number of times, a for
loop is generally best. Otherwise, use a while
loop. This is not a hard-and-fast rule but it is a good rule-of-thumb. For example, you could write
unsigned int v;
unsigned int c;
for(c = 0; v; v >>= 1) c += v & 1;
but I think most C programmers would write this as
unsigned int v;
unsigned int c;
c = 0;
while(v) { c += v & 1; v >>= 1; }
Or, as another example, if you're going to read until the end of a file then you should use a while
loop. Thus
FILE *fp;
fp = fopen(path, "r");
while(fgets(buf, max, fp) != NULL) { /* something */ }
instead of
FILE *fp;
for(fp = fopen(path, "r"); fgets(buf, max, fp) != NULL; ) { /* something */ }
Now, reaching into religious territory, this is why I prefer while(1)
as the right way to do an infinite loop over for(;;)
.
Hope that helps.
这篇关于循环用C - 为()或while() - 这是最好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!