Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loops in C - for() or while() - which is BEST?

for() or while() - which is BEST?

for (i=1; i<a; i++)
  /* do something */

OR

i=1;
while (i<a) {
/* do something */
i++;
}
like image 433
aks Avatar asked Feb 11 '10 06:02

aks


People also ask

Which loop is better for or while in C?

While loops and for loops are equally efficient. I remember my computer teacher from highschool told me that for loops are more efficient for iteration when you have to increment a number. That is not the case. For loops are simply syntactically sugared while loops, and make iteration code faster to write.

Which loop is better for or while?

Using for: % Time elapsed: 0.0010001659 seconds. Using while: % Time elapsed: 0.026000023 seconds. The main reason that While is much slower is because the while loop checks the condition after each iteration, so if you are going to write this code, just use a for loop instead.

Which loop is best in C?

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.

WHY IS for loop better than while loop?

The major difference between for loop and while loop is that in the case of for loop the number of iterations is known whereas in the case of the while loop number of iterations is unknown and the statement will run until the condition is proved false.


1 Answers

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.

like image 66
jason Avatar answered Nov 15 '22 07:11

jason