Re: how can you improve this function?
From: Matthieu Villeneuve (matthieu_at_nospam.matthieu-villeneuve.net)
Date: 11/18/03
- Next message: Matthieu Villeneuve: "Re: how can you improve this function?"
- Previous message: Gerry Quinn: "Re: Programming Language Productivity: The Stupidity of Programmers"
- In reply to: Thomas Matthews: "Re: how can you improve this function?"
- Next in thread: Corey Murtagh: "Re: how can you improve this function?"
- Reply: Corey Murtagh: "Re: how can you improve this function?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Matthieu Villeneuve: "Re: how can you improve this function?"
- Previous message: Gerry Quinn: "Re: Programming Language Productivity: The Stupidity of Programmers"
- In reply to: Thomas Matthews: "Re: how can you improve this function?"
- Next in thread: Corey Murtagh: "Re: how can you improve this function?"
- Reply: Corey Murtagh: "Re: how can you improve this function?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|