graywolf
graywolf

Reputation: 7500

For vs While for finding last item in linked list

Should I use for or while loop for finding last item in linked list?

Item * last = list;
for (; last->next; last = last->next) ;

versus

Item * last = list;
while (last->next)
{
    last = last->next;
}

it's 2 lines versus 5 and it seems almost equally readable. Why I always see only the while form?

Upvotes: 2

Views: 269

Answers (4)

Jerry Coffin
Jerry Coffin

Reputation: 490148

If you're going to compare line counts, at least try to do it reasonably accurately. The for loop should be formatted more like this:

Item * last = list;
for (; last->next; last = last->next) 
    ;

This makes it much more apparent that the for loop is (intentionally) controlling an empty statement. The while loop can be written more like this:

Item * last = list;
while (last->next)
    last = last->next;

If we insist that we need braces around even a single controlled statement, they still end up with the same line count for each:

Item * last = list;
for (; last->next; last = last->next) {
    ;
}

vs:

Item * last = list;
while (last->next) {
    last = last->next;
}

So, the difference in line count was due solely to the formatting you chose to use (and, particularly, the fact that you didn't use the same or even similar formatting for the two).

Others have already pointed out that these are likely to compile to identical code, and I agree that you can (and should) expect this to be true with any reasonable compiler.

That leaves style and readability as the sole differentiation. In this respect, the for loop is clearly preferable. A few people who've apparently never recovered from early exposure to BASIC (or possibly FORTRAN) insist that for loops should be used only for simple counted loops like those supported in BASIC and FORTRAN. Applying this limitation to C or C++ is simply wrong-headed and silly. C's for loop was made much more versatile for a good reason, and placing purely artificial limits on it does nobody any favors at all.

Simple rule of thumb: if you're actually using at least two out of the three clauses in the for loop's header, it's better to use a for loop than a while loop. A for loop with an empty body is roughly similar to a constructor with an empty body: it may be unfamiliar and confusing to people who don't know the language, but to anybody who actually knows the language, it's trivial and as familiar as the fingers on their hand.

Upvotes: 2

gsamaras
gsamaras

Reputation: 73366

Because the is used when you know how many steps you are going to make. Moreover, this loop can have its variables private (the iterator).

The is used until a condition is meet. You can simulate all loops with this loop.

However, as one can easily see, both can be used, since they are equivalent.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

Why I always see only the while form?

There are two reasons why the while form is preferred:

  • Loops with empty bodies are not as readable as loops that have at least one statement in their body - Programmers new to C find the semicolon after the for loop confusing, or even miss the semicolon altogether, not understanding what is going on. There is no such confusion about the while loop.
  • for loops with loop variable declared outside the loop provide a strong indication that you actually want a while loop - Loop variables are scoped to the body of the loop. When you declare them outside the loop, it is because you want the ending value of the loop variable among the results of your loop.

Upvotes: 5

Sam Varshavchik
Sam Varshavchik

Reputation: 118340

The two loops are logically equivalent, and, most likely, will end up getting compiled to identical code.

Use whichever one you like.

Upvotes: 4

Related Questions