Sujet : Re: do { quit; } else { }
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 14. Apr 2025, 11:16:11
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtin99$vu24$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 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
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
The C equivalant is this:
for (i = a; i <= b; ++i)
stmt
Differences:
* Fortran has an official loop index variable 'i'. C doesn't; a loop can
look like this:
for (x=0; y=1; z=2)
* 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!)
* C needs you to specify how to increment the loop index
* Fortran allows an arbitrary number of statements in the loop body.
C allows only one; multiple statements require a compound statement.
WRT FORTRAN _loops_ I (faintly) only recall a syntax ambiguity "by
design" (and jump labels); certainly not something to advocate.
There was ambiguity due to spaces not being significant. Or rather, there was potential for certain typos not being detectable:
do100i=1,10 # do-loop
do100i=1.10 # assign 1.10 to 'do100i'
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.
Of course, in printed material, reserved words simply used bold type, and it looked gorgeous. The reality on a computer was rather different.
Janis