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.
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");
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.
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);
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);
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;
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);
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 :-)
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; ...
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; ...
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);
Kenny McCormack wrote:
Bart won't like the way you've abused C's "for" statement syntax to your >>own devious ends...
This seems an unnecessarily complicated loop. Even in C, it could be
written more cleanly.
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.
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.
In article <vtrvc6$mjoi$1@dont-email.me>,
Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
...
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;
Indeed. This is the common case - where you are looking for something, and if you find it, you break out of the loop (one way or another). If you
don't find it, then the loop terminates "normally" - and you need to catch that case.
for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
if (n != -1) printf("Found key at entry %d\n",n);
Bart won't like the way you've abused C's "for" statement syntax
to your own devious ends...
(But I like it)
On Fri, 18 Apr 2025 13:58:08 +0000, Kenny McCormack wrote:
In article <vtrvc6$mjoi$1@dont-email.me>,
Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
...
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;
Indeed. This is the common case - where you are looking for something, and >> if you find it, you break out of the loop (one way or another). If you
don't find it, then the loop terminates "normally" - and you need to catch >> that case.
for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
if (n != -1) printf("Found key at entry %d\n",n);
Bart won't like the way you've abused C's "for" statement syntax
I've long since stopped caring what Bart does or doesn't like. :-)
to your own devious ends...
I'll give you another devious one :-)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n",nc);
As do I. It is succinct, complies with the syntax of the C language,
and achieves the desired algorithmic goal.
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, [...]
So while 0-based can be more troublesome, it can still be a language
problem rather a 0-based one.
for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
On Fri, 18 Apr 2025 16:07:03 +0100, bart wrote:
for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
using goto label would be as this
n=NUM_ENTRIES
L: if(!((n >= 0) && (node[n] != key))) goto Ex
--n; goto L;
Ex:
On 24.04.2025 22:43, Rosario19 wrote:
On Fri, 18 Apr 2025 16:07:03 +0100, bart wrote:
for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
using goto label would be as this
n=NUM_ENTRIES
L: if(!((n >= 0) && (node[n] != key))) goto Ex
--n; goto L;
Ex:
You are just showing a low-level transformation of a higher-level
construct. (I'm sure the professional folks here are familiar with
such transformations.)
You are not [seriously] suggesting to use that 'goto' based form
instead of the 'for' loop, do you?
On Fri, 18 Apr 2025 16:07:03 +0100, bart wrote:
for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
using goto label would be as this
n=NUM_ENTRIES
L: if(!((n >= 0) && (node[n] != key))) goto Ex
--n; goto L;
Ex:
for (n = NUM_ENTRIES; (n >= 0) && (node[n] != key); --n) continue;
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 148:49:49 |
Calls: | 10,383 |
Calls today: | 8 |
Files: | 14,054 |
D/L today: |
2 files (1,861K bytes) |
Messages: | 6,417,760 |