Michael S <
already5chosen@yahoo.com> writes:
On Sun, 04 May 2025 07:31:11 -0700
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
Michael S <already5chosen@yahoo.com> writes:
>
On Tue, 15 Apr 2025 11:30:24 +0100
bart <bc@freeuk.com> 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.