Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: bart Newsgroups: comp.lang.c Subject: Re: Loops (was Re: do { quit; } else { }) Date: Mon, 21 Apr 2025 23:54:21 +0100 Organization: A noiseless patient Spider Lines: 102 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 22 Apr 2025 00:54:23 +0200 (CEST) Injection-Info: dont-email.me; posting-host="d044c27b80dbbae3d03ebae8688adc0f"; logging-data="3383145"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19oT0wKpnj5E+CVhcH/iO8R" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:C9hVTOXTTaJ5ceBcJ2LQr7WNjN8= In-Reply-To: Content-Language: en-GB Bytes: 4940 On 21/04/2025 22:06, Waldek Hebisch wrote: > bart wrote: t iteration goes over all elements in the hash table. > > BTW2: When looking at 'for' loop you are supposed to see pattern, > without need to track all steps. That's one of the disadvantages of using the same, often /inappropriate/ keyword for every kind of pattern. This is the original C again: for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); } This is the version that my visualisation tool produced (where C source is visualised in my syntax); posted earlier but it's worth posting again: p := pSchema^.trigHash.first while p do sqlite3DeleteTrigger(db, ref Trigger(p^.data)) p := p^.next od While the C version looks intimidating, this looks much cleaner and simpler (expanding those macros helped). > The intent of 'for' is to iterate over some collection. Each of > A, B, C is needed to know the collection. Sure, but ALL ON THE SAME LINE? All within the same parentheses? Putting multiple things on the same line is usually frowned upon. Look at these ludicrous examples: for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {....} I /think/ the last two are simple iterations! Notice that that last bit of the last example is nothing to do with the mechanics of the iteration; it's to do with the body. >> Especially A, which is only executed once, after which it's no longer >> part of the loop. > > Without A you do not know range of iteration. I've seen plenty of for-loops that start with 'for (;...)'; that doesn't seem to worry anyone! Which is not surprising; nobody seems to care anything here. >> I expect you also either think that: >> >> for (ch = 0; ch <= 255; ++ch) >> >> is better than: 'do ch = 0, 255', or is much more tolerant of it than I am. > > In 1975 C version was better: it allowed flexible loops using very > simple compiler. How complicated was a 1950s Fortran compiler? A 1964 BASIC interpreter? My own 1981 compiler implemented in 8KB? All supported such loops. There is no excuse. This is a Pascal program with such a loop: program loop(output); var c:integer; begin for c := 0 to 255 do writeln(c); end. I can run it with a toy Pascal compiler that is 22KB (of x64 code). > But saying that C is bad rarely brings someting new, people here > know most of C warts and have ways to cope with them. If they > thought that some other language is really better they would use > it instead of C. FYI, I do probably majority of my coding in > different languages. And yet, even within the limitations of C, people favour writing appalling code, when it could be much cleaner. So (1) they don't care; and (2) try to justify that bad code as being better. Here's that visualisation example above, converted into C: p = pSchema->trigHash.first; while (p) { sqlite3DeleteTrigger(db, ref Trigger(p->data)); p = p->next; } Compare with the original C at the top.