tobold.org

correct • elegant • free

△ Comp.lang.c △

◅ Bug Hunt Help!

Where would I use ^ ? ▻

loop terminaters and efficiency/style

In article <3pfldh$39h@newsstand.cit.cornell.edu>,
Marduk <awh1@Cornell.edu> wrote:
>Which is faster:
>
>a) for (i=0; i< 6;i++)
>b) for (i=0; i<=5;i++)
>
>?

If there's any difference between the "natural" translations (there
won't be on many processors), a good compiler will convert both to the
more efficient form.

>Which is better programming style?

a) is the usual idiom, as it fits well with zero-based arrays.

    #define DAYS_IN_WEEK 7
    char *daynames[DAYS_IN_WEEK];
    for (i = 0; i < DAYS_IN_WEEK; ++i) {
    ...

[ For unimportant pedantic reasons, I prefer ++i over i++ when the
expression is used only for its side-effect. ]

Bearing in mind the comments in the FAQ, question 17.18, I suggest you
always use this form.

Tim.
--
Tim Goodwin        | "X.400(84)... conformance can reduce
Public IP Exchange | real interoperability." -- Steve Kille

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.c
Subject: Re: loop terminaters and efficiency/style
Date: 18 May 1995 18:31:34 +0100
Organization: PIPEX, 216 Science Park, Cambridge, England
Message-ID: <3pg09m$hrh@pipe.pipex.net>
References: <3pfldh$39h@newsstand.cit.cornell.edu>

△ Comp.lang.c △

◅ Bug Hunt Help!

Where would I use ^ ? ▻