| Deutsch English Français Italiano |
|
<vttsac$3ao6b$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: Janis Papanagnou <janis_papanagnou+ng@hotmail.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 17:49:31 +0200
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <vttsac$3ao6b$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
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Apr 2025 17:49:32 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cfbbbdc6aad15e46bd05d465e8b67418";
logging-data="3498187"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fBDGwstlciu/i6ws1Zeks"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:vQ/qwkkmNvHPC2EkyBa6YElMX2s=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <vtrvc6$mjoi$1@dont-email.me>
Bytes: 3285
On 18.04.2025 00: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);
In "C" (and with low-level "C" arrays starting at 0) I find iterations
always a bit crude _if started from the end_. Anyway.
I'd like to remind another standard method (just for good measure)...
Adding the searched element at the end of the array - where you have
to _take provisions that there is storage_, of course - like
node[N] = key;
for (i=0; node[i] != key; i++)
// ...whatever...
if (i==N) // ...not found...
else // ...found at i...
with the nice property to have just one test to perform in the loop.
Janis
>
>> I've applied this method in many C and (vaguely) C-like languages.
>> Any language with a "for" type loop, where you can check the value after
>> the loop can avail themselves of this method.
>
> I concur :-)
>