Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

the loop control

 
 
Joe keane
Guest
Posts: n/a
 
      10-29-2011
Which is better?

int fa(int fool, struct foo *foob)
{
int ct;
struct foo *foop;

...

ct = fool;
foop = foob;

while (--ct >= 0)
{
...(foop)
foop++;
}

...
}

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
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-29-2011
On 10/30/11 10:38 AM, Joe keane wrote:
> Which is better?


Start by defining better!

> int fa(int fool, struct foo *foob)
> {
> int ct;
> struct foo *foop;
>
> ...
>
> ct = fool;
> foop = foob;
>
> while (--ct>= 0)
> {
> ...(foop)
> foop++;
> }
>
> ...
> }
>
> int fb(int fool, struct foo *foob)
> {
> struct foo *foop;
> struct foo *fooe;
>
> ...
>
> foop = foob;
> fooe = foob + fool;
>
> while (foop< fooe)
> {
> ...(foop)
> foop++;
> }
>
> ...
> }



--
Ian Collins
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      10-29-2011
On 2011-10-29, Joe keane <(E-Mail Removed)> wrote:
> Which is better?


Neither. Firstly, you're neglecting to take advantage of parameters being
local variables. Do you need fool and foob to retain the values
they had on entry into the function?

int fa(int fool, struct foo *foob)
{
while (fool-- > 0) {
/* process object referenced by foob */
foob++;
}
}

> while (--ct >= 0)


Getting into the habit of coding a descending loop this way will bite
you if the loop counter is ever an unsigned type. (Hopefully a compiler will
warn you that the comparison is always true because of the range of the type.)

If you're going to introduce an extra integer index variable anyway, consider
array indexing.

for (ct = 0; ct < fool; ct++) {
... foob[i] ...
}

This way you avoid introducing an extra pointer just to keep your
foob pointer stable.

If this array of struct foo is used frequently in the program,
you might want to encapsulate it better, so you don't have to keep
passing two parameters everywhere just to represent one object.

A null-terminated array is a another possibility. This is like
a null-terminated string, but of course the null element is a pointer.
The argument array passed into main is this way.

int fa(struct foo *foob)
{
for (; *foob != NULL; foob++) {
/* process foob */
}
}
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      10-30-2011
http://www.velocityreviews.com/forums/(E-Mail Removed) (Joe keane) writes:

> Which is better?


<snip>

No.
 
Reply With Quote
 
Philip Lantz
Guest
Posts: n/a
 
      10-30-2011
On Sat, 29 Oct 2011, Joe keane wrote:

> Which is better?
>
> ...


int fc(int fool, struct foo *foob)
{
...

for (int i = 0; i < fool; i++)
{
...(foob[i])
}

...
}
 
Reply With Quote
 
Joe keane
Guest
Posts: n/a
 
      10-31-2011
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.

>Do you need fool and foob to retain the values they had on entry into
>the function?


It helps to retain sanity.

e.g.

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

for (j = 0; j < len; j++)
{
fob->a = fq(...);
fob++;
}

...

for (j = 0; j < len; j++)
{
fob->b = fr(...);
fob++;
}

...
}

Oops!
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-31-2011
On 10/31/2011 11:21 AM, Joe keane 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.
>
>> Do you need fool and foob to retain the values they had on entry into
>> the function?

>
> It helps to retain sanity.
>
> e.g.
>
> int f2(int len, struct foo *fob)
> {
> ...
>
> for (j = 0; j < len; j++)
> {
> fob->a = fq(...);
> fob++;
> }
>
> ...
>
> for (j = 0; j < len; j++)
> {
> fob->b = fr(...);
> fob++;
> }
>
> ...
> }
>
> Oops!


I agree; that's a context where you need to keep the original value of
fob. To get compiler enforcement of the idea that it should not be done,
declare fob to as "struct foo * const fob".

However, I don't see anything wrong with

int f2(int len, struct foo *fob)
{
while(len--)
{
fob->a = fq(...);
fob++;
}
}

I don't see that as being any harder to debug, any harder to maintain,
and I don't see it as being necessary to retain my sanity (though some
would undoubtedly argue that it's too late for me to worry about that ).
 
Reply With Quote
 
Joe keane
Guest
Posts: n/a
 
      10-31-2011
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. It says 'i don't need the array base and index, just give me
the pointer'.

All style, you should get the same machine code. But i've seen too many
cases where the code that looks 'faster' actually is! I mean it's nice
to say 'the optimizer will take care of this', but then it doesn't hurt
to help it some, by not making code more complicated than it should be.

Anyway readability is key; to me code like

ct = len;
fop = fob;
while (--ct >= 0)
{
...(fop)
fop++;
}

is so idiomatic that it's easiest.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      10-31-2011
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.

-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
 
jgharston
Guest
Posts: n/a
 
      10-31-2011
James Kuyper wrote:
> Kaz Kylheku wrote:
> > Firstly, you're neglecting to take advantage of parameters being
> > local variables.

> I don't like that "feature" of C.


It's not a feature of C, it's a feature of subroutines.
I can't think of any programming language where the
passed parameters are not local to the subroutine.

I'm happy for somebody to enlighten me, though.

JGH
 
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