Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Loops (was Re: do { quit; } else { }) Date: Sat, 10 May 2025 06:43:38 -0700 Organization: A noiseless patient Spider Lines: 103 Message-ID: <86plggzilx.fsf@linuxsc.com> References: <87a58mqt2o.fsf@nosuchdomain.example.com> <20250413072027.219@kylheku.com> <20250415153419.00004cf7@yahoo.com> <86h62078i8.fsf@linuxsc.com> <20250504180833.00000906@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sat, 10 May 2025 15:43:40 +0200 (CEST) Injection-Info: dont-email.me; posting-host="53a0400bfdbb5d49833963bc2e4763c3"; logging-data="3745339"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ath1zERcifPev51CfMhOV3qR/HP9Pchk=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:LHgbXn/sRUnwB+Up1EEH6x1cIUI= sha1:kmFcjvDZ3PgIdQB563yxkSxd1js= Xref: csiph.com comp.lang.c:393317 Michael S writes: > On Sun, 04 May 2025 07:31:11 -0700 > Tim Rentsch wrote: > >> Michael S writes: >> >>> On Tue, 15 Apr 2025 11:30:24 +0100 >>> bart wrote: >>> >>>> Let me ask you this: what exactly is the point of the 'while' >>>> statement in C? Since it can always be trivially be written as: >>>> >>>> for (;cond;) >>>> >>>> It seems to that most use cases (initialise, check exit condition, >>>> change something that affects the letter), would suit 'for' better. >>>> >>>> But since 'for' then becomes overloaded, there ought to be a >>>> dedicated feature for simple iteration. So it seems the solution >>>> is as a I suggested above. >>> >>> I suspect that 'while' loop is here in C because Dennis Ritchie >>> wanted 'do .. while() ' and thought that if the keyword is here >>> anyway than why not reuse it? >> >> According to K&R, all of the basic control structures in C -- if, >> while, for, do, and switch (and listed in that order) -- were >> provided in BCPL, though not using the same syntax as in C,. >> >>> In the hindsight, probably a mistake. >> >> I admit I don't understand this reaction. > > I don't like reuse of [keywords]. Neither of 'while' nor of 'break' nor > of 'static' (even more so in C++) nor use of 'long' modifier both for > integer and for floating-point types. > Double meaning of 'while' adds unnecessary mental load for a code > reader. Not a lot of it, but still unnecessary. Let me take these one at a time, roughly in order of most overloaded first. My aim here is to offer some perspectives that may alleviate the negative effects you feel. A new use for 'static' was added in C99, having to do with parameter array declarators. This new use has nothing to do with the earlier uses. All I can offer here is that these new uses don't occur very often, and are never necessary (in the same sense that 'restrict' is never necessary). Also it isn't easy to think of a good substitute word that might be given for this use of 'static', so maybe the extra effort needed could be seen as a positive. Starting already in pre-C99, 'static' has two uses that are somewhat different from each other, those uses being one, outside of any function definition, and two, local to a particular function body. Do you find the two kinds of circumstances need extra mental effort? To me the meaning in the two cases is the same but the applications are different, in much the same way that 'int x;' at file scope and 'int x;' in block scope are both the same and different. So even though the how 'static' is used is different in the two cases, for consistency with other parts of declarations it seems better just to use 'static' for both. For 'long', maybe it would help to think of 'long' as an independent axis, sort of like 'volatile'. I think most programmers wouldn't have trouble with two independent axes, such as {binary, decimal} and {fixed-point, floating-point}. What modifiers would you use if had to have different words for integer types and floating-point types? Here I think using 'long' (or maybe 'wide'?) for both is more natural than having different words for the two cases. For 'break', to me all uses of 'break' do the same thing: they exit one level of control structure. It is perhaps odd that 'break' cannot be used with 'if', but other than that it seems needlessly redundant to have different kinds of 'break' for each of the various kinds of control structure. And if a function is so complicated that it isn't immediately apparent which control structure is being exited, that is an indication that the code needs revising, and not just because of problems with break. Similarly with 'while', both uses of 'while' mean the same thing: a single test to control whether the controlled loop continues or exits. We might think of the two kinds of loops as being different, but as far as 'while' goes it is doing the same thing whether it is at the top or at the bottom. I hope you have found these comments helpful. On a larger scale, it might help to think in general terms rather than being distracted by low-level details. > Also having just one form of loop with pre-condition strikes me as more > elegant. Since elegance is strongly subjective, I have no logical > arguments in support of me feelings. In a recent posting I gave some statistics about the three different kinds of looping controls (while,for,do/while). do/while loops were almost 20% of all loops, and more than a quarter of the two kinds of loops other than for(). Besides being a more pragmatic choice, I think having the three kinds of loops be distinct in the language is a better choice, because how we think of the different kinds of loop is different, and it's helpful to have those differences be readily apparent in the program, rather than needing to reconstruct them by looking at code around the loop control fragment.