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

Liste des GroupesRevenir à cl c 
Sujet : Loops (was Re: do { quit; } else { })
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.c
Date : 14. Apr 2025, 13:18:39
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtiuf0$18au8$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/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
(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.)

On 14.04.2025 12:16, bart wrote:
On 14/04/2025 05:23, Janis Papanagnou wrote:
On 13.04.2025 18:39, bart wrote:
[...]
>
   for(let i = 1; i <= 36; i++) {
>
Is it that hard to provide a proper for-loop where you don't have to
spell out every single detail?
>
You mean like in Algol 68 or maybe Simula? (Both are interesting.)
 
I mean like pretty much every language that hasn't copied C's for-loop
syntax. For example Lua:
 
   for i = a, b                  # iterate over a..b inclusive
     s1
     s2
     ...
   end

Obviously there are many and there were many programming languages
that have other and better loop syntaxes than Fortran or Lua (if
above code is all that it provides).

 
Fortran managed it in the 1950s!
>
I obviously don't recall FORTRAN good enough to have memorized any
"good" feature. But as (mostly?) an anachronism it anyway doesn't
matter any more (to me).
 
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;

It's simple, and a clean formalism.

 
The C equivalant is this:
 
    for (i = a; i <= b; ++i)
        stmt
 
Differences:
 
* Fortran has an official loop index variable 'i'.

You are saying that you could not use 'j' ? (This is certainly
different from my memories.) - Anyway, having dedicated variables
for integer types is sick in itself, also if used without loop.

C doesn't; a loop can look like this:
 
    for (x=0; y=1; z=2)

I don't need to tell you that in "C" you can use arbitrary
increments/decrements, not just 1, and arbitrary conditions,
and more than one initializer and more increment/decrement
statements, and also omit unnecessary parts.

If all you want is a "+1" increment you can do that in the loop
body where i is used, and initialize it at the declaration point

  int i = a;
  for (; i <= b ; ) f(i++)

which of course would be written with a 'while'

  int i = a;
  while (i <= b) f(i++)

The "C" syntax has actually a flexibility that is very valuable.
(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; 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.)

 
* 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.)

 
* 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...

[...]
 
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? Aren't you used to
that from typically capitalized CPP entities? - But okay. You
have your own languages where you can ignore other people and
implement your personal preferences.)

 
Of course, in printed material, reserved words simply used bold type,
and it looked gorgeous. The reality on a computer was rather different.

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

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

* better readability (because of stropping!); an easy
  differentiation of syntax structure and user defined
  entities (variables, conditions, functions, ...)

* all loop-parts (but DO and OD) optional; no typing
  overhead, clear constructs, sensible defaults for
  omitted parts

* implicit bracketing (no extra braces nor additional
  keywords necessary) with short keywords DO OD

* loop variables need no declaration

* loop variable scope is just the loop

The specific Genie compiler-interpreter supports also
a (non-standard) UNTIL part. (Just for completeness.)


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   ...

where value lists, counted parts, conditional parts can
be taken and interspersed as necessary (also multiple
times) or just be omitted.

It's also noteworthy that Simula's logic is applicable
also for floating point variables without the danger
of accumulated rounding/truncation issues.


And you think Fortran's loop is a good paragon? *sigh*

Janis


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