Re: Loops (was Re: do { quit; } else { })

Liste des GroupesRevenir à cl c 
Sujet : Re: Loops (was Re: do { quit; } else { })
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.c
Date : 14. Apr 2025, 16:22:37
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtj97r$1i3v3$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 22 23 24 25 26 27 28 29 30
User-Agent : Mozilla Thunderbird
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 solved by Algol68, and now is common in many languages that don't use braces. (Braces are the equivalent of BEGIN-END.)

[...]
>
Algol68 also ignores white space, at least within identifiers. That
would have caused clashes with reserved words, so that 'stropping' had
to be employed, resulting in ugly and fiddly-to-type source code.
 (Huh? You have issues with keywords in caps?
Yes I have issues with *constantly* switch from lower case all-caps. It is tedious, error prone and the result is ugly code.
That doesn't bother you? Then you have remarkably low tolerance. But again, notice that most languages these days do not distinguish reserved words like that.

There were various stropping methods used in the past, some
better some worse. The Genie compiler-interpreter uses all caps
and it looks great; makes the keywords stand out and makes the
code structure very clear and perfectly legible! (YMMV.)
 And you have a powerful, yet minimalistic syntax; you "pay"
only for what you need and can express anything. Examples...
    # infinite loop
   DO s1; s2 OD
    # loops n times (no variable necessary)
   TO n DO s1; s2 OD
    # infinite loop with counted index available
   FOR i DO s1; s2(i) OD
    # loop n times with variable value available
   FOR i TO n DO s1; s2(i) OD
    ... and so on until...
    # full arithmetic loop (start, increment, end)
   FOR i FROM k BY 3 TO m DO s1; s2(i) OD
    # full arithmetic loop with condition
   FOR i FROM k BY 3 TO m WHILE cond(i) DO s1; s2 OD
    # only condition (no arithmetic)
   WHILE cond DO s1; s2 OD
    # only condition (with preparing statements)
   WHILE s1; s2; cond DO s1; s2 OD
(Note: Algol68 comments need a trailing # too.)
I have all those in my syntax, in fact the above would be legal sytax in my langage (since it is case-insensitive), apart from minor details:
* I use := instead of FROM
* I use TO/BY rather than BY/TO
* I don't have embedded WHILE, instead I have embedded WHEN, which affects only that iteration. (WHILE will stop the loop when false. Note that Algol68 did not have 'break'; it has to use 'goto'.)
Also, DO, TO and WHILE are independent features in my language, but that's an internal detail.
I also have some extras that are not in Algol68, such as being able to iterate over values (of a list etc), or an optional ELSE clause.
However, I'm not sure why we're talking about Algol68: my complaint was about why some languages chose to copy C's crude for-loop syntax, but it it was considered primitive even in 1972.

What you can see here are a couple of advantages with
this sort of loop;
 * one formal construct (not many variants) to express
   all the common loop controls
As I said, I have the same features, but it is multiple constructs. So having 'one construct' is just a gimmick.

* better readability (because of stropping!); an easy
   differentiation of syntax structure and user defined
   entities (variables, conditions, functions, ...)
That's rubbish too. I can also write:
   FOR i := a TO b DO
if I want (but I don't as it's just a pain). It works because it is case-insensitive.

* all loop-parts (but DO and OD) optional; no typing
   overhead, clear constructs, sensible defaults for
   omitted parts
I have sensible defaults too! I also don't have annoying rules about semicolons, or problems with empty bodies:
    DO SKIP OD
I don't need 'SKIP'.
    DO
      s1;
      s2;
      s3
    OD
Notice s3 is special-cased, as it has no semicolon. This leads to fun and games when inserting, deleting, moving statements, or even temporarily commenting lines in or out. At least C gets this right! (Mostly)

The Simula loop, while not as rich as Algol 68's, has
also some distinct specifics; just a short example
    FOR z := 2, 3, 5, 7, 11, 13, 17, 19,
            21 STEP 2 UNTIL 99,
            z+1 WHILE p(z)  DO   ...
What is it demonstrating? Since it seems to be merely this:
   for z in X while p(z) DO
where X is any expression including such a constructor. I can do some of this stuff, but in my next language up. It is higher level than what we're discussing, and not even much about loops.

And you think Fortran's loop is a good paragon? *sigh*
It seems we could have kept this short.
I said that Fortran's original DO-loop was more advanced than C's for-loop.
You are saying that because there exist languages with more sophisticated loops, that that somehow excuses C's poor effort?

Date Sujet#  Auteur
4 Apr 25 * do { quit; } else { }625Thiago Adams
4 Apr 25 +* Re: do { quit; } else { }2bart
4 Apr 25 i`- Re: do { quit; } else { }1Thiago Adams
4 Apr 25 +* Re: do { quit; } else { }11Kaz Kylheku
4 Apr 25 i+* Re: do { quit; } else { }3Thiago Adams
4 Apr 25 ii`* Re: do { quit; } else { }2Kaz Kylheku
4 Apr 25 ii `- Re: do { quit; } else { }1Chris M. Thomasson
4 Apr 25 i+* Re: do { quit; } else { }4Kaz Kylheku
4 Apr 25 ii+* Re: do { quit; } else { }2Thiago Adams
4 Apr 25 iii`- Re: do { quit; } else { }1Thiago Adams
8 Apr 25 ii`- Re: do { quit; } else { }1candycanearter07
5 Apr 25 i`* Re: do { quit; } else { }3Janis Papanagnou
5 Apr 25 i +- Re: do { quit; } else { }1Janis Papanagnou
6 Apr 25 i `- Re: do { quit; } else { }1Michael S
4 Apr 25 +* Re: do { quit; } else { }608Tim Rentsch
4 Apr 25 i`* Re: do { quit; } else { }607Thiago Adams
6 Apr 25 i +* Re: do { quit; } else { }600Tim Rentsch
6 Apr 25 i i+* Re: do { quit; } else { }550Michael S
6 Apr 25 i ii`* Re: do { quit; } else { }549Tim Rentsch
6 Apr 25 i ii `* Re: do { quit; } else { }548Michael S
7 Apr 25 i ii  `* Re: do { quit; } else { }547Tim Rentsch
7 Apr 25 i ii   `* Re: do { quit; } else { }546Michael S
7 Apr 25 i ii    +* Re: do { quit; } else { }542bart
8 Apr 25 i ii    i`* Re: do { quit; } else { }541David Brown
8 Apr 25 i ii    i `* Re: do { quit; } else { }540bart
8 Apr 25 i ii    i  +* Re: do { quit; } else { }535David Brown
8 Apr 25 i ii    i  i`* Re: do { quit; } else { }534bart
8 Apr 25 i ii    i  i +* Re: do { quit; } else { }78Tim Rentsch
8 Apr 25 i ii    i  i i`* Re: do { quit; } else { }77bart
8 Apr 25 i ii    i  i i +* Re: do { quit; } else { }74Tim Rentsch
8 Apr 25 i ii    i  i i i`* Re: do { quit; } else { }73bart
9 Apr 25 i ii    i  i i i `* Re: do { quit; } else { }72Tim Rentsch
9 Apr 25 i ii    i  i i i  `* Re: do { quit; } else { }71bart
9 Apr 25 i ii    i  i i i   +- Re: do { quit; } else { }1Chris M. Thomasson
9 Apr 25 i ii    i  i i i   +- Re: do { quit; } else { }1Chris M. Thomasson
9 Apr 25 i ii    i  i i i   `* Re: do { quit; } else { }68Tim Rentsch
10 Apr 25 i ii    i  i i i    +* Re: do { quit; } else { }63bart
10 Apr 25 i ii    i  i i i    i+* Re: do { quit; } else { }61Kaz Kylheku
10 Apr 25 i ii    i  i i i    ii+* Re: do { quit; } else { }2Michael S
10 Apr 25 i ii    i  i i i    iii`- Re: do { quit; } else { }1Kaz Kylheku
10 Apr 25 i ii    i  i i i    ii`* Re: do { quit; } else { }58bart
10 Apr 25 i ii    i  i i i    ii +* Re: do { quit; } else { }43Keith Thompson
10 Apr 25 i ii    i  i i i    ii i+* Re: do { quit; } else { }39bart
10 Apr 25 i ii    i  i i i    ii ii+* Re: Endless complaints [was Re: do { quit; } else { }]16bart
10 Apr 25 i ii    i  i i i    ii iii+* Re: Endless complaints [was Re: do { quit; } else { }]14Janis Papanagnou
11 Apr 25 i ii    i  i i i    ii iiii`* Re: Endless complaints [was Re: do { quit; } else { }]13bart
11 Apr 25 i ii    i  i i i    ii iiii +- Re: Endless complaints [was Re: do { quit; } else { }]1Keith Thompson
11 Apr 25 i ii    i  i i i    ii iiii +- Re: Endless complaints [was Re: do { quit; } else { }]1Kaz Kylheku
11 Apr 25 i ii    i  i i i    ii iiii `* Re: Endless complaints [was Re: do { quit; } else { }]10David Brown
11 Apr 25 i ii    i  i i i    ii iiii  `* Re: Endless complaints [was Re: do { quit; } else { }]9bart
11 Apr 25 i ii    i  i i i    ii iiii   +* Re: Endless complaints [was Re: do { quit; } else { }]5Michael S
11 Apr 25 i ii    i  i i i    ii iiii   i`* Re: Endless complaints [was Re: do { quit; } else { }]4bart
11 Apr 25 i ii    i  i i i    ii iiii   i `* Re: Endless complaints [was Re: do { quit; } else { }]3Michael S
11 Apr 25 i ii    i  i i i    ii iiii   i  +- Re: Endless complaints [was Re: do { quit; } else { }]1Janis Papanagnou
11 Apr 25 i ii    i  i i i    ii iiii   i  `- Re: Endless complaints [was Re: do { quit; } else { }]1bart
11 Apr 25 i ii    i  i i i    ii iiii   +- Re: Endless complaints [was Re: do { quit; } else { }]1David Brown
11 Apr 25 i ii    i  i i i    ii iiii   +- Re: Endless complaints1Tim Rentsch
11 Apr 25 i ii    i  i i i    ii iiii   `- Re: Endless complaints [was Re: do { quit; } else { }]1Keith Thompson
10 Apr 25 i ii    i  i i i    ii iii`- Re: Endless complaints [was Re: do { quit; } else { }]1Keith Thompson
10 Apr 25 i ii    i  i i i    ii ii`* Re: do { quit; } else { }22Keith Thompson
11 Apr 25 i ii    i  i i i    ii ii `* Re: do { quit; } else { }21bart
11 Apr 25 i ii    i  i i i    ii ii  `* Re: do { quit; } else { }20Keith Thompson
11 Apr 25 i ii    i  i i i    ii ii   `* Re: do { quit; } else { }19Michael S
11 Apr 25 i ii    i  i i i    ii ii    +- Re: do { quit; } else { }1David Brown
11 Apr 25 i ii    i  i i i    ii ii    +* Re: do { quit; } else { }16Kaz Kylheku
11 Apr 25 i ii    i  i i i    ii ii    i+* Re: do { quit; } else { }2bart
11 Apr 25 i ii    i  i i i    ii ii    ii`- Re: do { quit; } else { }1Keith Thompson
13 Apr 25 i ii    i  i i i    ii ii    i`* Re: do { quit; } else { }13Michael S
12 May 25 i ii    i  i i i    ii ii    i `* Re: do { quit; } else { }12Tim Rentsch
12 May 25 i ii    i  i i i    ii ii    i  `* Re: do { quit; } else { }11David Brown
12 May 25 i ii    i  i i i    ii ii    i   `* Re: do { quit; } else { }10Keith Thompson
13 May 25 i ii    i  i i i    ii ii    i    `* Re: do { quit; } else { }9David Brown
14 May 25 i ii    i  i i i    ii ii    i     `* Re: do { quit; } else { }8James Kuyper
14 May 25 i ii    i  i i i    ii ii    i      +* Re: do { quit; } else { }6Keith Thompson
14 May 25 i ii    i  i i i    ii ii    i      i+* Re: do { quit; } else { }4James Kuyper
14 May 25 i ii    i  i i i    ii ii    i      ii`* Re: do { quit; } else { }3David Brown
14 May 25 i ii    i  i i i    ii ii    i      ii +- Re: do { quit; } else { }1Kaz Kylheku
15 May 25 i ii    i  i i i    ii ii    i      ii `- Re: do { quit; } else { }1James Kuyper
14 May 25 i ii    i  i i i    ii ii    i      i`- Re: do { quit; } else { }1David Brown
14 May 25 i ii    i  i i i    ii ii    i      `- Re: do { quit; } else { }1Tim Rentsch
11 Apr 25 i ii    i  i i i    ii ii    `- Re: do { quit; } else { }1Keith Thompson
6 May 25 i ii    i  i i i    ii i`* Re: do { quit; } else { }3Tim Rentsch
6 May 25 i ii    i  i i i    ii i `* Re: do { quit; } else { }2Keith Thompson
6 May 25 i ii    i  i i i    ii i  `- Re: do { quit; } else { }1Tim Rentsch
10 Apr 25 i ii    i  i i i    ii `* Re: do { quit; } else { }14Kaz Kylheku
10 Apr 25 i ii    i  i i i    ii  +* Re: do { quit; } else { }11bart
10 Apr 25 i ii    i  i i i    ii  i+* Re: do { quit; } else { }2Kaz Kylheku
11 Apr 25 i ii    i  i i i    ii  ii`- Re: do { quit; } else { }1bart
11 Apr 25 i ii    i  i i i    ii  i+* Re: do { quit; } else { }6Tim Rentsch
11 Apr 25 i ii    i  i i i    ii  ii`* Re: do { quit; } else { }5Keith Thompson
11 Apr 25 i ii    i  i i i    ii  ii `* Re: do { quit; } else { }4Tim Rentsch
11 Apr 25 i ii    i  i i i    ii  ii  `* Re: do { quit; } else { }3Keith Thompson
11 Apr 25 i ii    i  i i i    ii  ii   +- Re: do { quit; } else { }1bart
5 May 25 i ii    i  i i i    ii  ii   `- Re: do { quit; } else { }1Tim Rentsch
11 Apr 25 i ii    i  i i i    ii  i+- Re: do { quit; } else { }1Keith Thompson
11 Apr 25 i ii    i  i i i    ii  i`- Re: do { quit; } else { }1Keith Thompson
10 Apr 25 i ii    i  i i i    ii  +- Re: do { quit; } else { }1bart
10 Apr 25 i ii    i  i i i    ii  `- Re: do { quit; } else { }1Kaz Kylheku
11 Apr 25 i ii    i  i i i    i`- Re: do { quit; } else { }1Tim Rentsch
9 May 25 i ii    i  i i i    `* Re: do { quit; } else { }4Bonita Montero
9 May 25 i ii    i  i i i     `* Re: do { quit; } else { }3Richard Heathfield
9 Apr 25 i ii    i  i i +- Re: do { quit; } else { }1Richard Damon
9 Apr 25 i ii    i  i i `- Re: do { quit; } else { }1David Brown
9 Apr 25 i ii    i  i `* Re: do { quit; } else { }455David Brown
8 Apr 25 i ii    i  +- Re: do { quit; } else { }1Tim Rentsch
9 Apr 25 i ii    i  `* Re: do { quit; } else { }3Ike Naar
8 Apr 25 i ii    `* Re: do { quit; } else { }3Tim Rentsch
6 Apr 25 i i`* Re: do { quit; } else { }49Michael S
7 May 25 i `* Re: do { quit; } else { }6Wuns Haerst
6 Apr 25 +- Re: do { quit; } else { }1Lawrence D'Oliveiro
6 Apr 25 +- Re: do { quit; } else { }1David Brown
18 Apr 25 `- Re: do { quit; } else { }1Mikko

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal