Sujet : Re: Loops (was Re: do { quit; } else { })
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 21. Apr 2025, 00:29:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vu401g$reom$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
User-Agent : Mozilla Thunderbird
On 20/04/2025 23:36, Keith Thompson wrote:
bart <bc@freeuk.com> writes:
for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
}
I might write it like this:
for ( p = sqliteHashFirst(&pSchema->trigHash);
p != NULL;
p = sqliteHashNext(p) )
{
sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
}
I have certain preferences (spaces around most operators, explicit
comparison to NULL, willingness to split long lines) that other C
programmers may or may not share.
I rarely see complex loops split over multiple lines (mainly when they get so long that they'd overflow the line, but they can still be complex enough before then).
But the thing is, once you split it into multiple lines, then there is little advantage over using a regular 'while' loop:
p = sqliteHashFirst(&pSchema->trigHash);
while (p != NULL)
{
sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
p = sqliteHashNext(p) )
}
The same number of lines, but now:
* The one-time initialisatioin is isolated and out of the way
* The execution order is more natural: the increment follows the body
as it does at runtime.
* It's clearly a while loop which repeats until some pointer is NULL
(Note there can be minor differences in behaviour with statements like 'continue', or variable scope when the 'for' declares its variables.)
It keeps it even more together, which you seem to like.
That's something you invented. I find it ugly, and I presume you'd
agree. The fact that you think that someone else would prefer it
indicates that you don't understand how other people think.
AFAIK it is legal C code, and I invented it because somebody said things that belong together should be together in one place. However, I have seen actual examples like that, in for-headers that that use comma-separated expressions.
I'd rather not write `for (ch in 0..255)` because it's a syntax error.
It's a syntax error because the form doesn't naturally exist in C; you'd have to emulate using macros, which is a poor solution.
You have the luxury of using your own language.
That 'ch in 0..255' form or close equivalent is supported by that long set of languages I listed earlier. It's not my invention, I just copied it.
It is just something that is desirable. Look again at the C version: it looks off. (At least, you'd use a shorter loop index!)
I said I tried one like C's, and it was never used. There is enough
flexibility in the rest to deal with anything that comes up.
It was never used by whom?
By me. One use-case was porting code from C, but I didn't do much of that either.
If you don't like C-style for loop, they absolutely should not
exist in a language for which you are, if I understand correctly,
the sole implementer and the sole user.
But I hear so much about how wonderful it is, how multi-purpose, how indispensible, how superior to an ordinary 'for' (admittedly from people who don't have a choice) that I didn't want to miss out!
| Date | Sujet | # | | Auteur |
| 23 Apr 26 | … | | | |
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal