Sujet : Re: Loops (was Re: do { quit; } else { })
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 16. Apr 2025, 15:31:38
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtof09$2db8v$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Mozilla Thunderbird
On 16/04/2025 13:08, Michael S wrote:
On Wed, 16 Apr 2025 12:32:13 +0100
bart <bc@freeuk.com> wrote:
>
But never, mind, C's for-loop will still be the most superior to
everybody here. I'd have an easier time arguing about religion!
>
Who exactly said that it is superior? Surely not me.
I think, most posters here would agree with my stance that C for() is
non-ideal. esp. for writer, but good enough.
And it has a minor advantage of being more clear for casual readers
than most "proper" counting loop.
Well, there is less ambiguity about the iteration range. But this is something that C's 0-based nature has helped create (see below).
When counting loop is written as C
for() loop, a casual reader does not have to consult the manual about
meaning of the 1st, 2nd and 3rd (if present) parameters.
No, you have to analyse the loop instead:
for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
The first challenge is to isolate the condition! That means carefully counting those ; and , characters. Then figuring out what's going on.
These three clearly are not simple iterations, yet they are all start 'for'. I'd write the third (at least) like this:
i = 0;
while (zLine[i] && IsSpace(zLine[i]) {++i}
Now constrast with a language where you KNOW that 'for' isn't going to be doing anything whacky. You know there will be a loop index name and some limits, or a collection of values to loop over:
for index in firstvalue..lastvalue
for x in collection
I don't know about you, may be your memory is perfect. Mine is not.
Even with python, a language that I use more often than once per year,
remembering whether range(3) means (0,1,2) or (0,1,2,3) is an effort.
Much more so with (modern) Fortran, that I read very rarely. In case of
Fortran, it certainly does not help that the principle of do loop is the
same as for loop in Matlab/Octave that I use regularly, but the order of
parameters differs.
When languages used to be 1-based, it was easy. Then 0-based came along, and typically iteration changed from 1..N inclusive, to 0..N-1 inclusive.
Now, a range may or may not be inclusive. In Python, 0-based, it isn't, so range(10) or range(0,10) means 0..9 inclusive.
Oh, now you could interpret a written above as statement of superiority
of C syntax. So, no, it is not. Those are *minor* points.
Here's some C code to print the elements of an array:
static char* table[] = {"one", "two", "three", "four", "five"};
for (int i=0; i<sizeof(table)/sizeof(table[0]); ++i)
printf("%d %s\n", i, table[i]);
And here is how I'd write it using the 'minor' advantages of my syntax:
static []ichar table = ("one", "two", "three", "four", "five")
for i, s in table do
println i, s
end
To me, the differences in that for-loop are like chalk and cheese.
(Note that the latter is 1-based, but it is not apparent in the code; the outputs are numbered 1 to 5. The C is zero-based, and that has to be specified. The outputs are numbered 0 to 4.
Moreover, I could have specified a different array base, and the loop remains unchanged.)