Sujet : Re: Loops (was Re: do { quit; } else { })
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 21. Apr 2025, 12:57:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vu5bqp$230jl$2@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
User-Agent : Mozilla Thunderbird
On 21/04/2025 04:16, Kaz Kylheku wrote:
On 2025-04-20, bart <bc@freeuk.com> wrote:
But the thing is, once you split it into multiple lines, then there is
little advantage over using a regular 'while' loop:
The advantage may be little, but there is some.
p = sqliteHashFirst(&pSchema->trigHash);
while (p != NULL)
{
sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
p = sqliteHashNext(p) )
}
- In a while loop, if you "continue", it will jump to the top of
the loop, without executing the step.
I mentioned this. I would not call it an advantage, since it depends on the behaviour you want. It's just different.
- The loop controls are not gathered in one place.
The loop controls being .... the last line of the loop body?
WHILE doesn't have the concept of a 'blob' that is a formal increment or step expression. Maybe that assignment to 'p' in the above example occurs in multiple places within the body.
My own 'while' has such an optional step expression. It was intended to show how C could have implemented linked list traversal, since that's the go-to example of how flexible its 'for' is.
I played with two kinds of syntax:
while cond do
body
step
incr # 'incr' represents anything
end
while cond, incr do
body
end
When the experiment was over, I decided to keep that second syntax, and it is sometimes used. But I can't use it when the factors that will affect the next 'cond' are complex.
Currently, from 20-30% of while loops use that form, almost exclusively for linked list traversal.
It doesn't affect the normal while-loops. It doesn't impinge on normal for loops either.
(My version of 'continue' will not skip the step part, but it is also very rarely used.)
> - Because they are not gathered in one place, not only is it less> readable, but we cannot use while write a macro such as:
for_sqlite_hash (p, &pSchema->trigHash) {
if (some_condition_over(p))
continue; // doesn't stupidly repeat for the same p!
}
I can't write such macros at all. I'm not even sure what this does.
I'd call that a win!