Sujet : Re: do { quit; } else { }
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 15. Apr 2025, 01:35:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtk9k7$2fucn$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 15/04/2025 00:12, Kaz Kylheku wrote:
On 2025-04-13, bart <bc@freeuk.com> wrote:
The first time I encountered a C for loop used for non-numbers
was a watershed moment for me. I rubbed my eyes once or twice.
I first time I saw this:
for(i=0; i<N; ++)
was such a moment for me. I thought what crappy syntax is that? I'd been used to Algol and Pascal and even Fortran, and this looked prehistoric.
This would have been in either a book or magazine. I don't think my opinions about C ever picked.
(I see there's typo; I tend to leave those in to show the problems.)
"Wow, wait, you can do that?"
It might have been in the source code of an IRC client I was
compiling, where it had something like this:
for (node = list; node; node = node->next)
I realized, OMG, this is a great way of defining a for loop
construct. This is why for is the way it is.
But it's not a for-loop is it? This is a perfect candidate for a while-loop: you are repeating some body of code /while/ some expression is true.
Whereas 'for' implies a control variable which iterates over some bounds or values.
What C calls 'for' is little more than a crass feature like this:
KEYWORD(A; B; C) D;
which is roughly transformed to:
A;
while (B) {
{D}
C;
}
It's what you might use in a macro-assembler to crudely emulate HLL features.
So much better than the crap languages I used before which restricted a
dummy variable into going from one number to another.
I use these loops almost exclusively:
Endless loop
Repeat-N-times loop
While loop
Iterate over an integer range
Iterate over a collection of values
It is incredibly rate that I need anything else. However I do have some extra kinds of loops which are a combination:
docase do case ... end end
doswitch do switch ... end end
doswitchu/x special forms used for fast dispatch loops in
interpreters
I can't see the attraction of trying to pack as much arbitrary junk into a for-loop header as possible, which just makes it harder to figure what
the hell kind of loop it is.
That might have been the point after which I didn't look back.
Later I learned that much of the cool stuff around control flow
and expressions in C actually came from Lisp.
- Ternary if: (if test then else).
- Expressions having values, even assignments: (setq a (setq b (setq c 0)))
- Loops working with arbitrary data types.
For me most such stuff came from Algol68. Did that get it from Lisp? I guess nobody knows.
But let me ask you; in the presumably rare situations where you do actually want to iterate over 0 to N-1, what do feel about writing 13 or so tokens to express that instead of maybe 6?
You can create some crummy macro for it, sure, but how do you then feel about NEEDING to implement a fundamental feature in a language? A million programmers would have to the same thing in a 1000 different ways.
No, it isn't, especially if you don't need once-only evaluation of the
range points:
#define for_range (type, var, from, to) \
for (type var = from; var <= to; var++)
Anyway, languages should be programmable so you're not stuck with
the loop that is built into the parser.
Help! So for-loops aren't flexible enough, we need every programmer to devise their own whacky creations!
If I were to pick a non-C language for systems programming today, I
would take a serious look at Thomas Mertes' Seed 7, probably.
It lets you define new statement and such.
That is seriously restricted:
* It doesn't have 'goto' for a start
* It doesn't like using libraries other than those written as Seed7
* It is anyway built on top of C, in that it depends on libraries written in C ...
* ... and it compiles to C.
Also, despite it allowing you to define new syntax, I don't think I've seen any Seed7 program look like anything other than Seed7.