Path: ...!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: Checking the loop variable after the loop has ended (Was: Loops (was Re: do { quit; } else { })) Date: Fri, 18 Apr 2025 20:10:32 +0100 Organization: A noiseless patient Spider Lines: 79 Message-ID: References: <87fri68w2c.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 18 Apr 2025 21:10:33 +0200 (CEST) Injection-Info: dont-email.me; posting-host="225decbae7f6d1b66db568edbb209aee"; logging-data="3819852"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/7p+BW0ex6R9p2sGhMXBhu" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:aLJ+TKYoAcyVqY/KQs4MqyazZJE= In-Reply-To: Content-Language: en-GB Bytes: 3675 On 18/04/2025 19:33, Lew Pitcher wrote: > On Fri, 18 Apr 2025 13:58:08 +0000, Kenny McCormack wrote: > >> In article , >> Lew Pitcher wrote: >> ... >>>> for (i=0; i<10; i++) { code that might or might not break } >>>> if (i == 10) puts("It ended normally"); >>> >>> It's also a handy idiom for a compact list search loop >>> where the terminating condition is either end of the list, or >>> a matched entry. >>> If the cursor isn't the end-of-list marker, then it references >>> the matched entry; >> >> Indeed. This is the common case - where you are looking for something, and >> if you find it, you break out of the loop (one way or another). If you >> don't find it, then the loop terminates "normally" - and you need to catch >> that case. >> >>> for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue; >>> if (n != -1) printf("Found key at entry %d\n",n); >> >> Bart won't like the way you've abused C's "for" statement syntax > > I've long since stopped caring what Bart does or doesn't like. :-) That's fine. It's been far longer that I've given up caring what people think of my generated C code (They used to like turning up the warnings high enough for it to fail. Clearly that defined but unused label is a critical error.) > > >> to your own devious ends... > > > I'll give you another devious one :-) > > #include > #include > int main(void) > { > double nc; > > for (nc = 0; getchar() != EOF; ++nc) > ; > > printf("%.0f\n",nc); I'll give you this one. Finally something that is impossible to achieve with a simple and far more appropriate 'while': double nc; nc = 0; while (getchar() != EOF) ++nc; printf("%.0f\n",nc); > As do I. It is succinct, complies with the syntax of the C language, > and achieves the desired algorithmic goal. That's a pretty low bar. You can say the same about any of my generated, low-level C code, which is pretty much unreadable by any human. It's odd how some here have standards which stops them even declaring three related variables on the same line: double x, y, z; // fails guidelines But cramming as much crap as possible into a FOR-loop header, for no reason other than because you can, seems to be condoned.