Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Loops (was Re: do { quit; } else { }) Date: Sun, 20 Apr 2025 19:08:36 -0700 Organization: None to speak of Lines: 100 Message-ID: <87a58auuzf.fsf@nosuchdomain.example.com> References: <87ecxmv4t4.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 21 Apr 2025 04:08:59 +0200 (CEST) Injection-Info: dont-email.me; posting-host="9df2d817d671536e75ba887619a4c13a"; logging-data="1149846"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18eWb1CF3hiebyCo5UMQjG7" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:PvqIdrvs1pgUxK3nYxkKlrd3rr8= sha1:K3gCLZO2AmOLfgBcuNlk77J3nnk= Xref: csiph.com comp.lang.c:392767 bart writes: > On 20/04/2025 23:36, Keith Thompson wrote: >> bart 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) ) > } You prefer a while loop. That's a perfectly valid preference, one that you've repeated so often that it's **boring**. I still like the for loop. [...] > 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 also have seen C for loops written in ways I don't like. >> 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. Yes. >> 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!) It's something that *you* desire, which is perfectly fine. And the fact that others don't desire it seems to deeply offend you, which is weird. >>> 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! Again, stop misrepresenting what other people say. Wonderful? No, I don't recall anyone other than you using that word, and only sarcastically. Multi-purpose? Yes, absolutely. Indispensible? No, I don't believe anyone has made that claim, and it would be inaccurate. Superior to an ordinary 'for'? Certainly a lot of people think it is, because of its flexibility. (And I could argue that C-style for loops *are* "ordinary", since a number of languages have adopted them, but I won't, because it would be an argument about the precise meaning of "ordinary", which is both off-topic and not something I care about.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */