Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > the loop control

Reply
Thread Tools

the loop control

 
 
James Kuyper
Guest
Posts: n/a
 
      11-01-2011
On 11/01/2011 05:51 AM, Ike Naar wrote:
> On 2011-11-01, James Kuyper <(E-Mail Removed)> wrote:

....
>> The meaning of 'count' and 'point' do not change anywhere within the
>> following code:
>>
>> void zero_array(
>> int *point,
>> int count
>> ){
>> while(count-- > 0)
>> *point = 0;
>> }

....
> ... And actually the meanings of
> count and point *do* change, initially they describe the entire
> set of points, and later on they describe the set of points
> yet-to-be-processed.


No, those variables always describe the set of points to be processed;
the fact that this is the entire set at the beginning of the routine is
just a special case. It's not necessarily true even then:

int array[20];

zero_array(array+5, 10);
--
James Kuyper
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      11-01-2011
On Oct 31, 5:18*pm, Seebs <(E-Mail Removed)> wrote:
> On 2011-10-31, Joe keane <(E-Mail Removed)> wrote:
>
> > In article <(E-Mail Removed)>,
> > Kaz Kylheku *<(E-Mail Removed)> wrote:
> >>Firstly, you're neglecting to take advantage of parameters being
> >>local variables.

> > I don't like that "feature" of C.
> > It's harder to debug, it's harder to maintain.

>
> It doesn't matter. *It's there. *If you write code which acts like it's
> not, it sends up big red flags for any future maintainer of your code.


not if I'm the maintainer
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-01-2011
On 2011-10-31, Keith Thompson <(E-Mail Removed)> wrote:
> James Kuyper <(E-Mail Removed)> 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 / http://www.velocityreviews.com/forums/(E-Mail Removed)
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.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      11-01-2011
On 2011-11-01, Nick Keighley <(E-Mail Removed)> wrote:
> On Oct 31, 5:18?pm, Seebs <(E-Mail Removed)> wrote:
>> On 2011-10-31, Joe keane <(E-Mail Removed)> wrote:
>> > In article <(E-Mail Removed)>,
>> > Kaz Kylheku ?<(E-Mail Removed)> wrote:
>> >>Firstly, you're neglecting to take advantage of parameters being
>> >>local variables.
>> > I don't like that "feature" of C.
>> > It's harder to debug, it's harder to maintain.


>> It doesn't matter. ?It's there. ?If you write code which acts like it's
>> not, it sends up big red flags for any future maintainer of your code.


> not if I'm the maintainer


I have ended up maintaining a lot of code that was written by someone
who did not anticipate the possibility of other people touching the code.
I have, in my own code, gradually learned to write with the intent that
it should not require you to be me to maintain it or have a good sense for
what it's doing and why.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / (E-Mail Removed)
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.
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      11-01-2011
pete <(E-Mail Removed)> 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
 
Reply With Quote
 
Joe keane
Guest
Posts: n/a
 
      11-02-2011
Joe writes this code:

...
val = *frp++;
HAIRY_MACRO(val, qux);
...

It works fine and everyone's happy.

Bob comes along and says 'maybe we don't need "val", maybe it is used
only once', and he finds that it is.

He makes this change:

...
HAIRY_MACRO(*frp++, qux);
...

The change works fine, so no one notices.

Bob's like 'i'm a genius!'.

....

A year later, Jim is working on a big new project.

He changes:

#define HAIRY_MACRO(X, Y) \
(((X) & 0x8000) == 0 ? BIGMAC1(Y) : BIGMAC2(Y))

to:

#define HAIRY_MACRO(X, Y) \
(((X) & 0x8000) == 0 ? BIGMAC1(Y) : \
((X) & 0x4000) == 0 ? BIGMAC2(Y) : BIGMAC3(Y))

We know the second bit is never set in the old code, so the only
possible problem is a bug in BIGMAC3.

The new code works, mostly... Worst yet, the old code works, mostly...
There is some glitch that no one can track down.

Jim invites his friends to look over the macros, and related functions,
but they find nothing wrong.

Jim spends a few hours debugging his favorite test program, trying to
pinpoint the problem. Then he gets it.

Jim's like 'oh! some a---hole put a postincrement in the macro
argument! who does that?!'.

[OBTW Bob is no longer with the company]
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-02-2011
On 11/ 2/11 04:36 PM, Joe keane wrote:
> Joe writes this code:
>
> ...
> val = *frp++;
> HAIRY_MACRO(val, qux);
> ...
>
> It works fine and everyone's happy.
>
> Bob comes along and says 'maybe we don't need "val", maybe it is used
> only once', and he finds that it is.
>
> He makes this change:
>
> ...
> HAIRY_MACRO(*frp++, qux);
> ...
>
> The change works fine, so no one notices.
>
> Bob's like 'i'm a genius!'.
>
> ....
>
> A year later, Jim is working on a big new project.
>
> He changes:
>
> #define HAIRY_MACRO(X, Y) \
> (((X)& 0x8000) == 0 ? BIGMAC1(Y) : BIGMAC2(Y))
>
> to:
>
> #define HAIRY_MACRO(X, Y) \
> (((X)& 0x8000) == 0 ? BIGMAC1(Y) : \
> ((X)& 0x4000) == 0 ? BIGMAC2(Y) : BIGMAC3(Y))


Reason number 248 for not using function-like macros.

--
Ian Collins
 
Reply With Quote
 
Joe keane
Guest
Posts: n/a
 
      11-02-2011
Do you like the follwing example better?

int f3(int len, struct foo *fob)
{
...

#ifdef DEBUG
printf("f3 before\n");
printf("[ ");
ct = len;
fop = fob;
while (--ct >= 0)
{
printf("<%d,%d> ", fop->e, fop->f);
fop++;
}
printf("]\n");
#endif

ct = len;
fop = fob;
while (--ct >= 0)
{
fop->alg |= FOO_ALG_ZYX;
gromlyana(fop);
fop->erg &= ~FOO_ERG_BAZ;
fop++;
}

#ifdef DEBUG
printf("f3 after\n");
printf("[ ");
ct = len;
fop = fob;
while (--ct >= 0)
{
printf("<%d,%d> ", fop->e, fop->f);
fop++;
}
printf("]\n");
#endif

...
}

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-02-2011
On Nov 1, 9:44*pm, Seebs <(E-Mail Removed)> wrote:
> On 2011-11-01, Nick Keighley <(E-Mail Removed)> wrote:
>
> > On Oct 31, 5:18?pm, Seebs <(E-Mail Removed)> wrote:
> >> On 2011-10-31, Joe keane <(E-Mail Removed)> wrote:
> >> > In article <(E-Mail Removed)>,
> >> > Kaz Kylheku ?<(E-Mail Removed)> wrote:


> >> >>Firstly, you're neglecting to take advantage of parameters being
> >> >>local variables.

>
> >> > I don't like that "feature" of C.
> >> > It's harder to debug, it's harder to maintain.

>
> >> It doesn't matter. ?It's there. ?If you write code which acts like it's
> >> not, it sends up big red flags for any future maintainer of your code.

>
> > not if I'm the maintainer

>
> I have ended up maintaining a lot of code that was written by someone
> who did not anticipate the possibility of other people touching the code.
> I have, in my own code, gradually learned to write with the intent that
> it should not require you to be me to maintain it or have a good sense for
> what it's doing and why.


oh I don't write obfuscated code (not intentionally anyway) but I
expect coding style to vary and don't get all bent out of shape over
minor style issues. And I think this is one of them. We don't pay a
penny for each variable usd so i don't think the occaisional,
technically, redundant one is automatically a "red flag" or a "code
smell".
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      11-02-2011
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++;
> }
>
> ...
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Getting a loop to activate a loop above it Byte Python 4 03-24-2006 03:04 AM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments