| Deutsch English Français Italiano |
|
<vtu2r4$3f353$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: bart <bc@freeuk.com>
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 18:40:52 +0100
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <vtu2r4$3f353$2@dont-email.me>
References: <vspbjh$8dvd$1@dont-email.me>
<87ikn3zg18.fsf@nosuchdomain.example.com> <vtqnv9$hf83$1@dont-email.me>
<87fri68w2c.fsf@nosuchdomain.example.com> <vtrrdi$1smfe$1@news.xmission.com>
<vtrvc6$mjoi$1@dont-email.me> <20250418081904.366@kylheku.com>
<vttsq5$3amn4$1@dont-email.me> <vttvlv$3dv0j$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Apr 2025 19:40:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="225decbae7f6d1b66db568edbb209aee";
logging-data="3640483"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fe0E8WySz0oyKV+C0TNhs"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:RkGVUPhcQtkzFd5iUkxDqHXnozI=
Content-Language: en-GB
In-Reply-To: <vttvlv$3dv0j$1@dont-email.me>
Bytes: 3365
On 18/04/2025 17:46, Janis Papanagnou wrote:
> 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).
That's never going to happen. Only, possibly, if you have to port a
1-based algorithm to C, without risking the bugs that will creep in if
you try and convert it to 0-based.
Sure, with 1-based, upward and downward counting loops can be more
symmetrical.
>
> 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.
I can use 0-base arrays in my language, and yes, for loops to iterate
over bounds could involve 0 and N-1:
for i 0 .. N-1 do
but they can also look like this:
for i in A.lwb .. A.upb do
or just:
for i in A.bounds do
for i, x in A do # iterate over bounds and values
'0' and 'N-1' or 'A.len-1', don't appear at all.
So while 0-based can be more troublesome, it can still be a language
problem rather a 0-based one.