(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