Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #12264 > unrolled thread
| Started by | jgk@panix.com (Joe keane) |
|---|---|
| First post | 2011-10-29 21:38 +0000 |
| Last post | 2011-11-02 01:25 -0700 |
| Articles | 10 on this page of 50 — 21 participants |
Back to article view | Back to comp.lang.c
the loop control jgk@panix.com (Joe keane) - 2011-10-29 21:38 +0000
Re: the loop control Ian Collins <ian-news@hotmail.com> - 2011-10-30 10:48 +1300
Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-29 22:10 +0000
Re: the loop control jgk@panix.com (Joe keane) - 2011-10-31 15:21 +0000
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 11:35 -0400
Re: the loop control jgharston <jgh@arcade.demon.co.uk> - 2011-10-31 10:27 -0700
Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-10-31 18:36 +0000
Re: the loop control Robert Wessel <robertwessel2@yahoo.com> - 2011-10-31 13:49 -0500
Re: the loop control Sjouke Burry <s@b> - 2011-10-31 18:49 +0000
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 15:07 -0400
Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-10-31 13:56 -0700
Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-10-31 17:18 +0000
Re: the loop control Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-01 04:35 -0700
Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
Re: the loop control Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-02 01:16 -0700
Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-31 19:23 +0000
Re: the loop control jgk@panix.com (Joe keane) - 2011-11-02 04:47 +0000
Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:07 +0200
Re: the loop control Ike Naar <ike@sverige.freeshell.org> - 2011-11-01 00:11 +0000
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 22:01 -0400
Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-11-01 02:51 +0000
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:23 -0400
Re: the loop control Ike Naar <ike@iceland.freeshell.org> - 2011-11-01 09:51 +0000
Re: the loop control pete <pfiland@mindspring.com> - 2011-11-01 05:34 -0500
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:30 -0400
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:35 -0400
Re: the loop control Hans Vlems <hvlems@freenet.de> - 2011-11-11 00:38 -0800
Re: the loop control jgk@panix.com (Joe keane) - 2011-11-02 03:36 +0000
Re: the loop control Ian Collins <ian-news@hotmail.com> - 2011-11-02 17:09 +1300
Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-11-02 03:56 -0700
Re: the loop control Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-31 21:38 +0000
Re: the loop control Acid Washed China Blue Jeans <chine.bleu@yahoo.com> - 2011-10-29 19:31 -0700
Re: the loop control Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-10-29 23:11 -0600
Re: the loop control "Philip Lantz" <prl@canterey.us> - 2011-10-29 22:47 -0700
Re: the loop control jgk@panix.com (Joe keane) - 2011-10-31 16:36 +0000
Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-31 19:30 +0000
Re: the loop control blp@cs.stanford.edu (Ben Pfaff) - 2011-10-31 12:38 -0700
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 15:51 -0400
Re: the loop control blp@cs.stanford.edu (Ben Pfaff) - 2011-10-31 13:06 -0700
Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-10-31 14:17 -0700
Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:30 +0200
Re: the loop control jgk@panix.com (Joe keane) - 2011-11-03 21:29 +0000
Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-03 18:00 -0400
Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-11-04 00:43 +0000
Re: the loop control jgk@panix.com (Joe keane) - 2011-11-11 02:24 +0000
Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:20 +0200
Re: the loop control pete <pfiland@mindspring.com> - 2011-10-31 18:01 -0500
Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-02 01:20 +0200
Re: the loop control 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-02 01:25 -0700
Page 3 of 3 — ← Prev page 1 2 [3]
| From | Seebs <usenet-nospam@seebs.net> |
|---|---|
| Date | 2011-11-01 21:44 +0000 |
| Message-ID | <slrnjb0rb2.ng2.usenet-nospam@guild.seebs.net> |
| In reply to | #12409 |
On 2011-10-31, Keith Thompson <kst-u@mib.org> wrote: > James Kuyper <jameskuyper@verizon.net> writes: > [...] >> A compiler that warns whenever a loop condition can never be false would >> annoy those people who rely upon the while(1) idiom. > Yes, such compilers exist, and yes, they're annoying. A sufficiently clever compiler (hah) can distinguish between "a test which is always true" and "a literal number which is always true". -s -- Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! I am not speaking for my employer, although they do rent some of my opinions.
[toc] | [prev] | [next] | [standalone]
| From | Phil Carmody <thefatphil_demunged@yahoo.co.uk> |
|---|---|
| Date | 2011-10-31 23:30 +0200 |
| Message-ID | <87zkggj1ck.fsf@bazspaz.fatphil.org> |
| In reply to | #12400 |
James Kuyper <jameskuyper@verizon.net> writes:
> On 10/31/2011 03:38 PM, Ben Pfaff wrote:
> > Kaz Kylheku <kaz@kylheku.com> writes:
> >
> >> On 2011-10-31, Joe keane <jgk@panix.com> wrote:
> >>> Anyway readability is key; to me code like
> >>>
> >>> ct = len;
> >>> fop = fob;
> >>> while (--ct >= 0)
> >>> {
> >>> ...(fop)
> >>> fop++;
> >>> }
> >>>
> >>> is so idiomatic that it's easiest.
> >>
> >> As pointed out upthread, it's an infinite loop should ct ever happen
> >> to be unsigned. Learning disability?
> >
> > It seems likely to me that the compiler would warn that the loop
> > condition cannot ever be true.
>
> "true" => "false".
>
> A compiler that warns whenever a loop condition can never be false would
> annoy those people who rely upon the while(1) idiom.
I see a clear subset relationship separating constant expressions and
expressions that always have the same value. The compiler _must_ be
able to distinguish the two, so it seems a shame if it's unable to
make use of that distinction in situations like these.
Phil
--
Unix is simple. It just takes a genius to understand its simplicity
-- Dennis Ritchie (1941-2011), Unix Co-Creator
[toc] | [prev] | [next] | [standalone]
| From | jgk@panix.com (Joe keane) |
|---|---|
| Date | 2011-11-03 21:29 +0000 |
| Message-ID | <j8v134$3ug$1@reader1.panix.com> |
| In reply to | #12398 |
I use it all the time.
The idiom
while (--ct >= 0)
...
has the nice property that if ct is negative coming in [almost], the
body is not executed, which is probably what you want.
And similar:
for (j = upp; j < low; j++)
...
e.g.
a = 3;
b = 2;
for (j = a; j < b; j++)
...
You'd be rather surprised if this executed four billion times.
If you need unsigned for a loop counter, there must be some reason for
doing so.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-11-03 18:00 -0400 |
| Message-ID | <4EB30EED.6020102@verizon.net> |
| In reply to | #12627 |
On 11/03/2011 05:29 PM, Joe keane wrote: > I use it all the time. > > The idiom > > while (--ct >= 0) > ... > > has the nice property that if ct is negative coming in [almost], the > body is not executed, which is probably what you want. A property it shares with while(ct-- > 0) which, in addition to the minor advantage of being one character shorter, also works as intended even if ct is unsigned. ... > If you need unsigned for a loop counter, there must be some reason for > doing so. That is correct.
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2011-11-04 00:43 +0000 |
| Message-ID | <20111103203052.351@kylheku.com> |
| In reply to | #12627 |
On 2011-11-03, Joe keane <jgk@panix.com> wrote:
> I use it all the time.
>
> The idiom
>
> while (--ct >= 0)
> ...
This is not an idiom; the meaning of this construct is exactly what
the rules for that combination of symbols say it is.
> has the nice property that if ct is negative coming in [almost], the
Almost, yes. What if it is INT_MIN already?
It also has the property that if ct is zero, it is not executed,
but you didn't catch that because it's not obvious, see below.
> body is not executed, which is probably what you want.
This has the stupid property of testing a value that is not the one
which came in. This makes it awkward to think about.
For what values of ct is the loop executed? It is ct - 1 which is tested, not
ct.
while (ct - 1 >= 0)
which is the same thing as
ct >= 1
which is the same as
ct > 0
Aha! Well, doh, why don't you use THAT condition in the fist place???
while (ct-- > 0) {
}
This still has the property of not executing the loop if ct is negative
coming in. It's algebraically equivalent, get it?
Furthermore, maybe you don't want to unconditionally decrement the variable,
whether or not the loop body executes!
If ct comes in as 0 is it okay to make it -1? That's fine if ct has no
next use after the loop.
Variations on the following are generally better:
for (; ct > 0; ct--) {
}
The loop guard controls the loop AND the increment step, which is part
of the loop.
This still has the virtue that the loop is not executed when ct comes
in as zero or negative. Furthermore, it has the virtue that ct is not
touched either if it is zero or negative. (Furthermore, it won't
decrement down from INT_MIN.)
> And similar:
>
> for (j = upp; j < low; j++)
> ...
This is not similar at all.
> If you need unsigned for a loop counter, there must be some reason for
> doing so.
Like maybe it occurs in code written by someone else you're maintaing?
[toc] | [prev] | [next] | [standalone]
| From | jgk@panix.com (Joe keane) |
|---|---|
| Date | 2011-11-11 02:24 +0000 |
| Message-ID | <j9i115$rfi$1@reader1.panix.com> |
| In reply to | #12648 |
I really don't care if you like
while (ct-- > 0)
versus
while (--ct >= 0)
Pick one you like! And know what issues may occur. Nothing is
foolproof here, and it can't be.
[toc] | [prev] | [next] | [standalone]
| From | Phil Carmody <thefatphil_demunged@yahoo.co.uk> |
|---|---|
| Date | 2011-10-31 23:20 +0200 |
| Message-ID | <874nyokgdb.fsf@bazspaz.fatphil.org> |
| In reply to | #12367 |
jgk@panix.com (Joe keane) writes:
> compare
>
> int fc(int len, struct foo *fob, struct bar *bab)
> {
> ...
>
> for (j = 0; j < len; j++)
> {
> fob[j].a = bab[j].x;
> fob[j].b = bab[j].y + 1;
> fob[j].c = bab[j].d;
> }
>
> ...
> }
>
> versus
>
> int fd(int len, struct foo *fob, struct bar *bab)
> {
> ...
>
> for (j = 0; j < len; j++)
> {
> fop = &fob[j];
> bap = &bab[j];
> fop->a = bap->x;
> fop->b = bap->y + 1;
> fop->c = bap->d;
> }
>
> ...
> }
>
> The first one looks like Pascal or Fortran. The second one looks more
> like C.
An absurd assertion.
> It says 'i don't need the array base and index, just give me
> the pointer'.
Since when has C been about ditching indices and arrays? Not in any version
of the language I've ever used. Then again, my use only dates back to 1989.
Phil
--
Unix is simple. It just takes a genius to understand its simplicity
-- Dennis Ritchie (1941-2011), Unix Co-Creator
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-10-31 18:01 -0500 |
| Message-ID | <4EAF28B8.7E25@mindspring.com> |
| In reply to | #12367 |
Joe keane wrote:
> Anyway readability is key; to me code like
>
> ct = len;
> fop = fob;
> while (--ct >= 0)
> {
> ...(fop)
> fop++;
> }
>
> is so idiomatic that it's easiest.
More idiomatic is:
while (ct-- != 0) {
;
}
It's a natural for counting down through an array.
unsigned array[10];
unsigned ct = sizeof array / sizeof *array;
while (ct-- != 0) {
array[ct] = ct;
}
--
pete
[toc] | [prev] | [next] | [standalone]
| From | Phil Carmody <thefatphil_demunged@yahoo.co.uk> |
|---|---|
| Date | 2011-11-02 01:20 +0200 |
| Message-ID | <878vnzig66.fsf@bazspaz.fatphil.org> |
| In reply to | #12416 |
pete <pfiland@mindspring.com> writes:
> Joe keane wrote:
>
> > Anyway readability is key; to me code like
> >
> > ct = len;
> > fop = fob;
> > while (--ct >= 0)
> > {
> > ...(fop)
> > fop++;
> > }
> >
> > is so idiomatic that it's easiest.
>
> More idiomatic is:
>
> while (ct-- != 0) {
Don't forget the pretty, but too cute for its own good:
while (ct --> 0)
> ;
> }
Yeah, that's the kind of smiley my snippet deserves!
Phil
--
Unix is simple. It just takes a genius to understand its simplicity
-- Dennis Ritchie (1941-2011), Unix Co-Creator
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2011-11-02 01:25 -0700 |
| Message-ID | <30013457.56.1320222329249.JavaMail.geo-discussion-forums@prln21> |
| In reply to | #12264 |
Joe keane於 2011年10月30日星期日UTC+8上午5時38分28秒寫道:
> Which is better?
>
> int fa(int fool, struct foo *foob)
> {
> int ct;
> struct foo *foop;
>
> ...
>
> ct = fool;
> foop = foob;
>
> while (--ct >= 0)
> {
> ...(foop)
> foop++;
> }
This is not useful for slow jump with JZ but no JNZ in ct.
if ct==1, --ct>=0 is TRUE.
> ...
> }
>
> int fb(int fool, struct foo *foob)
> {
> struct foo *foop;
> struct foo *fooe;
>
> ...
>
> foop = foob;
> fooe = foob + fool;
>
> while (foop < fooe)
> {
> ...(foop)
> foop++;
> }
>
> ...
> }
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.c
csiph-web