Sujet : Re: Checking the loop variable after the loop has ended (Was: Loops (was Re: do { quit; } else { }))
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 18. Apr 2025, 17:46:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vttvlv$3dv0j$1@dont-email.me>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 18.04.2025 17:57, bart wrote:
On 18/04/2025 16:24, Kaz Kylheku wrote:
On 2025-04-17, Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
[...]
[...]
for (n = NUM_ENTRIES - 1; ...
That looks like another inconsistency in the syntax (and another error
opportunity).
Since the loop for upward and downward iterations between 0 and N-1
inclusve would be:
for (i = 0; i < N; ++i)
for (i = N-1; i >= 0; --i)
'N-1' is what I would have to write; it's odd that C people have write
it too. Unless perhaps you do this:
for (i = N; i-- > 0;)
Here you now have a genuine 2-part loop!
I guess this is C being 'flexible', in being able to reinvent for-loops
each time.
It is perfectly fine to write your loops in "C" as
for (i = 1; i <= N; ++i)
for (i = N; i >= 1; --i)
(and access your array elements as [i-1] if you wish).
You should be aware that the problem you have here is *not* a "C"
"loop-problem", it's a problem of the low-level _"C" arrays_ you
have to use; they force you to define and use bounds of 0 and N-1
respectively if you want to use an array of N elements.
Try to address the issues to the correct source of "C" constructs.
The loop flexibility allows you to address such "array-crudeness",
it is not the source of the problem, neither here nor generally.
Janis