Deutsch   English   Français   Italiano  
<vttpqo$38nt7$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:07:03 +0100
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <vttpqo$38nt7$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Apr 2025 17:07:04 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="225decbae7f6d1b66db568edbb209aee";
	logging-data="3432359"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18NxcyawmFDzr6iFzaJwDCZ"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:TjI2Q+Z6nzCgpv1iDIMQcyvUWrI=
Content-Language: en-GB
In-Reply-To: <vtrvc6$mjoi$1@dont-email.me>
Bytes: 3376

On 17/04/2025 23:29, Lew Pitcher 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;
>    if (n != -1) printf("Found key at entry %d\n",n);

This seems an unnecessarily complicated loop. Even in C, it could be 
written more cleanly.

There is no 'end-of-list marker' that I can see; it is a straight linear 
search:

     n = -1;
     for (i = 0; i < NUM_ENTRIES; ++i)
         if (node[i] == key) { n = i; break;}

Now the loop header is a 100% routine iteration. Or, to avoid an 
auxiliary index (and still use -1 as the error marker rather than 
NUM_ENTRIES):

     for (n = NUM_ENTRIES-1; n >= 0; --n)
         if (node[n] == key) break;

BTW your example seems to have a bounds error (I assume the top element 
is at index NUM_ENTRIES-1). With a more complex approach approach, that 
is harder to spot.

(Below is how it might work in my language by utilising an 'else' part 
to the loop, here used to detect a key-not-found rather than key-found.)

     for i to node.len do
         exit when node[i] = key
     else
         println "Key not found"
     end