Re: how can you improve this function?

From: Matthieu Villeneuve (matthieu_at_nospam.matthieu-villeneuve.net)
Date: 11/18/03


Date: Tue, 18 Nov 2003 10:30:11 +0100


"Thomas Matthews" <Thomas_MatthewsHatesSpam@sbcglobal.net> wrote in message
news:w_9ub.21809$ln3.18703@newssvr33.news.prodigy.com...
> What about when one wants to repeat some functionality a given
> number of times:
>
> for (unsigned int i = 0; i < 20; ++i)
> {
> printf("Line number %d\n", i);
> }

That's right, iteration of a task a given number of times is the
other idiom of 'for'. (Although your example is rather a traversal
of the set of integers from 0 to 19 than a simple iteration of
the task 20 times, since the task uses the value of i).

> In my 30 years of programming experience, the "for" loop is used
> when there is a fixed number of iterations, initialization takes
> place before loop. Generally, in the context of a for loop the
> loop is to execute at least once (1 or more times). The "while"
> loop allows for the loop to not be executed the first time
> (zero or more iterations). The repeat or do-while loops are
> used when the code is executed 1 or more times.
>
> I really don't advise loop controls for various structures.
> The idiom I use is whatever is readable and conveys the meaning
> to the reader.

Absolutely, and I consider it wrong (or bad) to use 'for' for
anything that is not an iteration a given number of times or a
data structure traversal.

If the user naturally thinks "let's do that initialization, then
repeat that task while this test is true", then s/he should use
'while', not 'for'. If s/he thinks "let's do that task N times"
or "let's do that task for every element in this set", then
'for' is appropriate.

'for' and 'while' may have equivalent effects in programs, but
their semantics (in terms of human interpretation) are not
equivalent.

> Sometimes, in C, I will use a "for" loop as
> a while loop since the incrementing can be specified in the
> "for" statement whereas it must be specified _somewhere_ in
> the while loop.

Saving a line of code or two (by having initialization and
increment in the same line as the test) seems to me to be a quite
bad reason to use 'for' instead of 'while'. The main goal is to
get the code as easy to understand as possible, even if it
"costs" a few more lines.

--
Matthieu Villeneuve


Relevant Pages

  • Re: LOOP blows!
    ... At beginning of each iteration. ... All variables in the FOR clauses ... ] All variables are initialized in the loop prologue. ... Initialization is not assignment! ...
    (comp.lang.lisp)
  • Yow! LOOP macros are LOOPY!
    ... By relying entirely on procedure calls to express iteration, ... to but cleaner than C's FOR loop. ... other macros going around at the time other than MacLisp's ... (bind (vi (vector-ref v i))) ...
    (comp.lang.scheme)
  • Re: Polling, Interrupts, DMA, Synchronous, Asynchronous I/O Definitions
    ... the terminology is less useful than it might be. ... though a "message loop" could arguably be claimed to be ... considering in a particular iteration but what is true for _ALL_ ... watching the "polling" version eating up every single CPU cycle ...
    (alt.lang.asm)
  • Re: Histogram of character frequencies
    ... generated object code may simply be a loop in which elements are ... believe any C compiler anywhere would reject it. ... On the first iteration of the loop you test the end of file indicator ...
    (comp.lang.c)
  • Re: Matlab guide lines
    ... is used to generate cooccurance matrix for each iteration of for loop. ... disp('the sample co-occurance matrix is as follows');% GIVING SAME ...
    (comp.soft-sys.matlab)