Sujet : Re: Loops (was Re: do { quit; } else { })
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 16. Apr 2025, 06:19:18
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtnekn$1fogv$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 15.04.2025 12:30, bart wrote:
On 15/04/2025 08:17, Janis Papanagnou wrote:
On 14.04.2025 17:22, bart wrote:
On 14/04/2025 13:18, Janis Papanagnou wrote:
[...]
>
The "C" syntax has actually a flexibility that is very valuable.
>
The flexibility is the problem, because you have to specify the loop in
such excruciating detail. It is easy to make a mistake which results in
still legal code. And you now have to analyse each loop to see what kind
of loop it is:
>
Well, our opinions obviously differ. I just give you two examples
(and there's many variants of these) of not uncommon 'for' usages...
>
for (c=0; bitstr; c++)
bitstr &= bitstr-1;
I can't grok that.
It's a simple example of a loop where the condition is not depending
on the counting loop variable. - What is it that you don't understand
here? - That's one example where the "C" loop design decision allows
to express yet more sensible functionality than only simple loops with
counts of 1.
(In case you don't understand the actual operation - which is not the
point of this example - inspect, e.g., K&R and the footnotes there.)
If all you know (and can handle) is a simple counted loop then every
extension may appear to you being strange.
[ meaningless while/for stuff snipped ]
(One of your fundamental problems with "C" seems to be to understand
the 'for' keyword meaning only counted loops with one variable and a
simple +1 increment. - I can't help you here.)
[...]
>
Suppose you see 'i <= N' as the condition; is that '<=' intentional, or
is it a typo for '<'? It's impossible to tell.
>
(This is nonsense.)
You can't just say that without explanation. WHY it it nonsense?
It's nonsense because all programmers use the comparison operations
that are necessary; I don't know about you but if I do a primitive
arithmetic loop I start with 0 and compare < against N (the amount
of iterations) or start with 1 and compare <= against N; if that's
what is intended.
I've written code where the logic required comparisons of i < N-1
and also i <= N . So what? - Should I not do that because you are
getting confused when inspecting my code?
It's nonsense on so many levels.
If you have doubts ask the programmer or verify the logic. If you
are the programmer add a comment to make the intention clear. If
you are a project lead define coding standards, coach your team,
invent QA measures, do code reviews.
Take this:
for (i=0 i<=N; ++i)
Most such loops iterate over 0..N-1 inclusive, so would need "<".
(One of your fundamental problems with "C" seems to be to understand
the 'for' keyword meaning only counted loops with one variable and a
simple +1 increment. - I can't help you here.)
If you have some conditioning that "most" is the programming world
then I suggest to #define MOST_LOOP for (i=0 i<=N; ++i)
so that you don't make error and don't get confused respectively.
So, in
your opinion, is that <= a typo, or is the intention to iterate over
0..N inclusive (so N+1 iterations)?
It depends on the context.
Janis