Deutsch   English   Français   Italiano  
<vttsq5$3amn4$1@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 16:57:57 +0100
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <vttsq5$3amn4$1@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Apr 2025 17:57:58 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="225decbae7f6d1b66db568edbb209aee";
	logging-data="3496676"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18w7AOWi8HkLXchJggVtyAT"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Yy4ksmCqn8FbN6hY0tZ4jq6UYMw=
Content-Language: en-GB
In-Reply-To: <20250418081904.366@kylheku.com>
Bytes: 3236

On 18/04/2025 16:24, Kaz Kylheku wrote:
> On 2025-04-17, Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
>> On Thu, 17 Apr 2025 21:21:54 +0000, Kenny McCormack wrote:
>>
>>> In article <87fri68w2c.fsf@nosuchdomain.example.com>,
>>> Keith Thompson  <Keith.S.Thompson+u@gmail.com> wrote:
>>> ...
>>>> IMHO it doesn't much matter what the value is after the loop ends, but
>>>> any standard for a language with such a feature should either restrict
>>>> the scope to the loop, specify the value the variable has after the
>>>> loop, or explicitly say that it's unspecified or undefined.
>>>
>>> I frequently check the value of the loop variable after the loop has ended
>>> in order to determine if the loop ended "normally" or prematurely via
>>> "break".  E.g.,
>>>
>>>      for (i=0; i<10; i++) { code that might or might not break }
>>>      if (i == 10) puts("It ended normally");
>>
>> It's also a handy idiom for a compact list search loop
>> where the terminating condition is either end of the list, or
>> a matched entry.
>> If the cursor isn't the end-of-list marker, then it references
>> the matched entry;
>>
>>    for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
> 
> You're saying that the number of entries in the array isn't
> NUM_ENTRIES but NUM_ENTRIES + 1, since you're accessing node[0]
> and node[NUM_ENTRIES]
> 
> Don't you want:
> 
>    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.