| Deutsch English Français Italiano |
|
<vtj97r$1i3v3$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Loops (was Re: do { quit; } else { })
Date: Mon, 14 Apr 2025 16:22:37 +0100
Organization: A noiseless patient Spider
Lines: 308
Message-ID: <vtj97r$1i3v3$1@dont-email.me>
References: <vspbjh$8dvd$1@dont-email.me> <vt3iqh$2ka99$1@dont-email.me>
<vt5fed$ccri$1@dont-email.me> <vt5js2$g1t7$1@dont-email.me>
<20250409142303.00004645@yahoo.com> <87ikndqabc.fsf@nosuchdomain.example.com>
<20250410115501.000037a5@yahoo.com> <vt8ei8$2vn84$1@dont-email.me>
<20250410080629.532@kylheku.com> <vt94q5$3jjod$1@dont-email.me>
<vt9628$3hhr8$3@dont-email.me> <vtammh$174ev$1@dont-email.me>
<vtavn9$1dp7m$3@dont-email.me> <vtb8nv$1plb2$2@dont-email.me>
<vtba81$1qfbm$1@dont-email.me> <vtbc6o$1te2o$1@dont-email.me>
<vtbhjv$24api$1@dont-email.me> <vtbn2k$293r1$1@dont-email.me>
<vtc19j$2kqlj$1@dont-email.me> <87a58mqt2o.fsf@nosuchdomain.example.com>
<vtc7mp$2q5hr$1@dont-email.me> <vtcqf6$3j95s$1@dont-email.me>
<vtdh4q$b3kt$1@dont-email.me> <vtf7fe$1qtpg$1@dont-email.me>
<vtgfuf$31ug1$1@dont-email.me> <20250413072027.219@kylheku.com>
<vtgpce$39229$1@dont-email.me> <vti2ki$g23v$1@dont-email.me>
<vtin99$vu24$1@dont-email.me> <vtiuf0$18au8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 14 Apr 2025 17:22:36 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="d60bd519c9c1b35e0dadd9227d9a5963";
logging-data="1642467"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19YpBFmfwCCI02cQlQFazf3"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lxSYofLsyDOJvX5lijhLd+YFvbo=
In-Reply-To: <vtiuf0$18au8$1@dont-email.me>
Content-Language: en-GB
Bytes: 12113
On 14/04/2025 13:18, Janis Papanagnou wrote:
> (While there's some "C" stuff in here it contains a lot of non-"C"
> samples for comparison. So [OT]-sensible folks may want to skip this
> post.)
>> Fortran's loops looked like this:
>>
>> do 100 i = a, b
>> s1
>> s2
>> ...
>> 100 continue
>
> Okay, I see what you want; just two values (from, to).
>
> This Fortran stuff looks really sick (for my taste)! - The "do 100"
> sounds like iterating 100 times, line numbers, 'continue' keyword,
> and a list a,b meaning iteration over a range. - Can it be worse?
> (Later Fortran versions allow a slightly better syntax, but it's
> basically the same anachronistic crude syntax.)
>
> In _minimalistic_ languages I'd prefer, for example, a style like
> in Pascal (or in similar languages)
>
> for i := a to b do
> statement_block;
I'm not saying there's anything wrong with it. The point was that an A
to B loop existed in the 1950s; C came out in the 1970s!
>> * Fortran has an official loop index variable 'i'.
>
> You are saying that you could not use 'j' ?
No, it's just 'i' in this example.
> which of course would be written with a 'while'
>
> int i = a;
> while (i <= b) f(i++)
That would be a poor use of 'while'. And bizarre, to use 'while' for
iteration, but 'for' for loops that are best written as while!
> The "C" syntax has actually a flexibility that is very valuable.
The flexibility is the problem, because you have to specify the loop in
such excruciating detail. It is easy to make a mistake which results in
still legal code. And you now have to analyse each loop to see what kind
of loop it is:
Is it simple iteration? Does it count up or down? Are the limits
exclusive or inclusive? Is it something better off as a while-loop? Does
it do multiple things at the same time?
Is there a variable that can be considered to be 'the' loop index?
(Since there can be multiple such variables; even one variable may not
necessarily be an 'index', it could be a pointer.)
Suppose you see 'i <= N' as the condition; is that '<=' intentional, or
is it a typo for '<'? It's impossible to tell.
You can also say that a combination of labels, gotos and 'if' is even
more flexible! But C is supposed to be a HLL.
> (Even though I dislike its syntax, but that is a minor point in
> this context).
>
>>
>> * C needs you to provide the exact comparison op
>>
>> * C needs you to write the index 'i' three times (with no compiler check
>> that they're all identical!)
>
> They don't need to be identical, because you can write many more
> types of loops than simple counted loops. - This is actually an
> advantage;
This is the kind of error I was making all the time (writing the first
line, copying it, but forgetting to convert all the variables):
for(i=0; i < M; ++i)
for(j=0; j < N; ++i)
Here, it is not an advantage. I once asked David Brown how many of his
for-loops were simple iteration; I think he said 98.5%.
In my case it would be nearer 100%. It's amazing, with the amount of
lower-level coding I do, how little I need to use the flexibility of C's
'for'.
Anything unusual can generally be achieved with minor workarounds.
However with linked-list traversal, that's an example that people gave
of how flexible for-loops can be:
for (p = head; p; p = p->next)
I suggested that could have been better done with an extension to
'while'. I made a proof-of-concept in my language:
p := head
while p, p:=p.next do
This feature I've kept.
> it allows a lot more sensible loop code patterns than>
> those other primitive loops! - I'm astonished that your code
> pattern repertoire is so limited. (Or that you are arguing again
> just to contribute anything, however stupid that is.)
It sounds like you're being insulting again just to keep your hand in.
Why is it some people just cannot resist getting personal?
>>
>> * C needs you to specify how to increment the loop index
>
> Of course; another advantage! (And this is common in many
> programming languages, older and newer ones.)
You need to tell the computer how to count? OK!
>> * Fortran allows an arbitrary number of statements in the loop body.
>> C allows only one; multiple statements require a compound statement.
>
> Jesus! - At times I see you as a sensible communication partner,
> but in this sub-thread I've no hope. - I'll finish this post and
> stop responding to such stupid posts of yours...
I don't understand; what exactly is the problem here that you see as
nonsensical?
In C you have do this:
for(...)
stmt1;
Then you decide to add a new statement, but this doesn't work:
for(...)
stmt1;
stmt2;
(And has caused lots of errors.) You have to refactor into:
for(...) {
stmt1;
stmt2;
}
Then you decide you don't need stmt2 anymore, and may need to refactor
back. Or maybe you want to temporarily comment out stmt1:
for(...)
// stmt1;
But now you will be repeating whatever statement follows iself. It's a
dance.
If I understand you, you think it is idiotic for me to bring up what you
clearly perceive as a non-problem?
Even though it is one that also existed in Algol60 and Pascal, and was
========== REMAINDER OF ARTICLE TRUNCATED ==========