Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #150711 > unrolled thread

Comma operator in for() statements

Started byGeoff <geoff@invalid.invalid>
First post2020-03-21 19:51 -0700
Last post2020-03-27 08:02 -0700
Articles 20 on this page of 111 — 20 participants

Back to article view | Back to comp.lang.c


Contents

  Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-21 19:51 -0700
    Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 03:31 +0000
      Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-21 23:17 -0700
        Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 12:00 +0000
          Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 08:57 -0700
            Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 16:20 +0000
              Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 14:02 -0700
                Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 21:26 +0000
      Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-22 10:25 +0100
      Re: Comma operator in for() statements Richard Damon <Richard@Damon-Family.org> - 2020-03-22 08:27 -0400
        Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 16:31 +0000
    Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 00:33 -0400
    Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 12:23 +0000
      Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 05:47 -0700
        Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 14:17 +0000
          Re: Comma operator in for() statements felix@palmen-it.de (Felix Palmen) - 2020-03-22 15:55 +0100
            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 15:01 +0000
              Re: Comma operator in for() statements felix@palmen-it.de (Felix Palmen) - 2020-03-22 16:05 +0100
                Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 16:46 +0000
                  Re: Comma operator in for() statements felix@palmen-it.de (Felix Palmen) - 2020-03-22 18:14 +0100
                  Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 11:42 -0700
                    Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-22 22:03 +0000
                      Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 23:51 -0700
                        Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 14:37 +0000
                          Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 08:41 -0700
                            Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-23 16:53 +0100
                            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 16:31 +0000
                              Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-23 16:48 +0000
                                Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 09:53 -0700
                                  Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-23 18:59 +0100
                                    Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 11:23 -0700
                                      Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 19:51 +0000
                                        Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 14:28 -0700
                                      Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 13:09 -0700
                                        Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 13:20 -0700
                                          Re: Comma operator in for() statements scott@slp53.sl.home (Scott Lurndal) - 2020-03-23 20:26 +0000
                                          Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 13:55 -0700
                                            Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 15:43 -0700
                                              Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 16:27 -0700
                                                Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 17:13 -0700
                                                  Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 21:11 -0700
                                                Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-24 00:19 +0000
                                                Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 10:30 +0100
                                                  Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-24 03:07 -0700
                                                    Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 11:37 +0100
                                                  Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 11:18 +0000
                                                    Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 13:23 +0100
                                                      Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 14:53 +0000
                                                        Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-25 10:35 +0100
                                                          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-25 11:12 +0000
                                                      Re: Comma operator in for() statements Ike Naar <ike@sdf.lonestar.org> - 2020-03-26 19:21 +0000
                                                    Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-03-24 21:22 -0700
                                                      Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 06:46 -0700
                                                        Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-03-28 01:01 -0700
                                                          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-28 11:26 +0000
                                                            Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-03-28 14:22 -0700
                                                              Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 08:12 -0700
                                                                Re: Comma operator in for() statements luser droog <luser.droog@gmail.com> - 2020-04-02 12:21 -0700
                                                                  Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 19:43 -0700
                                                  Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-24 12:56 -0700
                                                  Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 23:44 +0000
                                              Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 17:10 -0700
                                          Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-24 10:17 +0100
                                  Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-23 18:13 +0000
                          Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 09:32 -0700
                            Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-23 19:06 +0100
                          Re: Comma operator in for() statements Richard Damon <Richard@Damon-Family.org> - 2020-03-23 13:23 -0400
                            Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 10:33 -0700
                              Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-23 18:34 +0100
                              Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-23 18:18 +0000
                          Re: Comma operator in for() statements Ike Naar <ike@sdf.lonestar.org> - 2020-03-23 22:29 +0000
                            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-23 23:13 +0000
              Re: Comma operator in for() statements richard@cogsci.ed.ac.uk (Richard Tobin) - 2020-03-22 15:17 +0000
            Re: Comma operator in for() statements nobody@example.org (Scott) - 2020-03-22 16:16 +0000
              Re: Comma operator in for() statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-23 07:22 -0700
              Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-25 15:10 +0100
                Re: Comma operator in for() statements Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-25 15:18 +0000
            Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 16:27 +0000
            Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 15:07 -0400
          Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 11:33 -0700
        Re: Comma operator in for() statements Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-03-24 20:25 +0300
          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 23:25 +0000
            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-25 01:55 +0000
              Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-25 11:01 +0000
            Re: Comma operator in for() statements Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-03-25 14:59 +0300
              Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 07:42 -0700
                Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-27 14:51 +0000
                  Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 19:20 -0700
          Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-24 20:01 -0400
            Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-25 02:05 +0000
              Re: Comma operator in for() statements Bart <bc@freeuk.com> - 2020-03-25 15:13 +0000
            Re: Comma operator in for() statements Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-03-25 15:05 +0300
              Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-25 10:04 -0400
      Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-22 14:49 +0100
    Re: Comma operator in for() statements Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-03-22 09:24 -0600
      Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 14:51 -0700
        Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 20:13 -0400
          Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-23 12:16 -0700
            Re: Comma operator in for() statements Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-23 13:16 -0700
            Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-23 17:16 -0400
              Re: Comma operator in for() statements scott@slp53.sl.home (Scott Lurndal) - 2020-03-23 22:40 +0000
    Re: Comma operator in for() statements Guillaume <message@bottle.org> - 2020-03-22 17:31 +0100
      Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 12:28 -0700
        Re: Comma operator in for() statements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 15:35 -0400
          Re: Comma operator in for() statements Geoff <geoff@invalid.invalid> - 2020-03-22 13:43 -0700
    Re: Comma operator in for() statements Les Cargill <lcargill99@comcast.com> - 2020-03-22 13:50 -0500
      Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 19:25 +0000
        Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-22 21:52 +0100
          Re: Comma operator in for() statements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-22 21:15 +0000
            Re: Comma operator in for() statements David Brown <david.brown@hesbynett.no> - 2020-03-23 09:17 +0100
    Re: Comma operator in for() statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 08:02 -0700

Page 1 of 6  [1] 2 3 4 5 6  Next page →


#150711 — Comma operator in for() statements

FromGeoff <geoff@invalid.invalid>
Date2020-03-21 19:51 -0700
SubjectComma operator in for() statements
Message-ID<m9kd7f94epq2eumphiui7gf2ubc6p8ah8e@4ax.com>
I'm aware of the hazards of using the comma operator in the tested
expression but I ran across this example and I think it should be
refactored, but how?

	for (count = 0, ent = master; ent; ent = ent->chain, count++)
				;


My first instinct is to write it:

		for (count = 0; ent; count++)
		{
			ent = master;
			ent = ent->chain;
		}

But is this correct?

[toc] | [next] | [standalone]


#150712

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-03-22 03:31 +0000
Message-ID<87h7yh6p6y.fsf@bsb.me.uk>
In reply to#150711
Geoff <geoff@invalid.invalid> writes:

> I'm aware of the hazards of using the comma operator in the tested
> expression but I ran across this example and I think it should be
> refactored, but how?
>
> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
> 				;
>
>
> My first instinct is to write it:
>
> 		for (count = 0; ent; count++)
> 		{
> 			ent = master;
> 			ent = ent->chain;
> 		}
>
> But is this correct?

No, the equivalent that is closes to your refactor is

  ent = master;
  for (count = 0; ent; count++)
  {
          ent = ent->chain;
  }

but maybe it's better to make all the part of the for loop relate to the
pointer and move the counting out of the brackets like this:

  count = 0;
  for (ent = master; ent; ent = ent->chain)
  {
          count++;
  }

But I would (a) declare the pointer for this loop, and (b) scratch the
{}s:

  int count = 0;
  for (T *ent = master; ent; ent = ent->chain)
      count++;

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#150714

FromGeoff <geoff@invalid.invalid>
Date2020-03-21 23:17 -0700
Message-ID<nlvd7f159dmfe83aet5pmivg9ahhja1mfc@4ax.com>
In reply to#150712
On Sun, 22 Mar 2020 03:31:01 +0000, Ben Bacarisse
<ben.usenet@bsb.me.uk> wrote:

>Geoff <geoff@invalid.invalid> writes:
>
>> I'm aware of the hazards of using the comma operator in the tested
>> expression but I ran across this example and I think it should be
>> refactored, but how?
>>
>> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
>> 				;
>>
>>
>> My first instinct is to write it:
>>
>> 		for (count = 0; ent; count++)
>> 		{
>> 			ent = master;
>> 			ent = ent->chain;
>> 		}
>>
>> But is this correct?
>
>No, the equivalent that is closes to your refactor is
>
>  ent = master;
>  for (count = 0; ent; count++)
>  {
>          ent = ent->chain;
>  }
>
>but maybe it's better to make all the part of the for loop relate to the
>pointer and move the counting out of the brackets like this:
>
>  count = 0;
>  for (ent = master; ent; ent = ent->chain)
>  {
>          count++;
>  }
>
>But I would (a) declare the pointer for this loop, and (b) scratch the
>{}s:
>
>  int count = 0;
>  for (T *ent = master; ent; ent = ent->chain)
>      count++;

Thank you Ben and James,

I probably shouldn't have brought the use of the comma operator in the
conditional expression into the topic but it was one of the only
references I could find in my quest for an solution. This is a piece
of ancient code dating from 1997.

I left the declarations out of my original post for brevity since the
only issue in my mind wasn't the actual operations on the variables
but the general stickiness of the 'for' loop.

I like this much better and I have included the declarations:

void SomeFunction(edict_t* ent)
{
		edict_t* master;	// edict_t contains member chain
		unsigned count;

... some conditions predicating execution of the loop...

			count = 0; 
			for (ent = master; ent; ent = ent->chain)
				count++;
...


-- 
Let the regression testing begin...

[toc] | [prev] | [next] | [standalone]


#150717

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-03-22 12:00 +0000
Message-ID<87a7487g5n.fsf@bsb.me.uk>
In reply to#150714
Geoff <geoff@invalid.invalid> writes:

> On Sun, 22 Mar 2020 03:31:01 +0000, Ben Bacarisse
> <ben.usenet@bsb.me.uk> wrote:
<cut>
>>... the equivalent that is closest to your refactor is
>>
>>  ent = master;
>>  for (count = 0; ent; count++)
>>  {
>>          ent = ent->chain;
>>  }
>>
>>but maybe it's better to make all the part of the for loop relate to the
>>pointer and move the counting out of the brackets like this:
>>
>>  count = 0;
>>  for (ent = master; ent; ent = ent->chain)
>>  {
>>          count++;
>>  }
>>
>>But I would (a) declare the pointer for this loop, and (b) scratch the
>>{}s:
>>
>>  int count = 0;
>>  for (T *ent = master; ent; ent = ent->chain)
>>      count++;
>
<cut>
> I left the declarations out of my original post for brevity since the
> only issue in my mind wasn't the actual operations on the variables
> but the general stickiness of the 'for' loop.

The declaration I was referring to was the one for ent.  I like the fact
that you can declare variables that are really only used in a for loop
to be declared there (see below).

> I like this much better and I have included the declarations:
>
> void SomeFunction(edict_t* ent)
> {
> 		edict_t* master;	// edict_t contains member chain
> 		unsigned count;
>
> ... some conditions predicating execution of the loop...
>
> 			count = 0; 
> 			for (ent = master; ent; ent = ent->chain)
> 				count++;
> ...

So I meant writing

 			for (edict_t *ent = master; ent; ent = ent->chain)
 				count++;
 
But this should be a function, surely?  There may be only one place
where you want to know the length of a chain, but I'd still probably
make it a function.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#150728

FromGeoff <geoff@invalid.invalid>
Date2020-03-22 08:57 -0700
Message-ID<rg2f7fdafeud5i7i9up8a1vqhbompq9s3k@4ax.com>
In reply to#150717
On Sun, 22 Mar 2020 12:00:52 +0000, Ben Bacarisse
<ben.usenet@bsb.me.uk> wrote:

>Geoff <geoff@invalid.invalid> writes:
>
>> On Sun, 22 Mar 2020 03:31:01 +0000, Ben Bacarisse
>> <ben.usenet@bsb.me.uk> wrote:
><cut>
>>>... the equivalent that is closest to your refactor is
>>>
>>>  ent = master;
>>>  for (count = 0; ent; count++)
>>>  {
>>>          ent = ent->chain;
>>>  }
>>>
>>>but maybe it's better to make all the part of the for loop relate to the
>>>pointer and move the counting out of the brackets like this:
>>>
>>>  count = 0;
>>>  for (ent = master; ent; ent = ent->chain)
>>>  {
>>>          count++;
>>>  }
>>>
>>>But I would (a) declare the pointer for this loop, and (b) scratch the
>>>{}s:
>>>
>>>  int count = 0;
>>>  for (T *ent = master; ent; ent = ent->chain)
>>>      count++;
>>
><cut>
>> I left the declarations out of my original post for brevity since the
>> only issue in my mind wasn't the actual operations on the variables
>> but the general stickiness of the 'for' loop.
>
>The declaration I was referring to was the one for ent.  I like the fact
>that you can declare variables that are really only used in a for loop
>to be declared there (see below).
>
>> I like this much better and I have included the declarations:
>>
>> void SomeFunction(edict_t* ent)
>> {
>> 		edict_t* master;	// edict_t contains member chain
>> 		unsigned count;
>>
>> ... some conditions predicating execution of the loop...
>>
>> 			count = 0; 
>> 			for (ent = master; ent; ent = ent->chain)
>> 				count++;
>> ...
>
>So I meant writing
>
> 			for (edict_t *ent = master; ent; ent = ent->chain)
> 				count++;
> 
>But this should be a function, surely?  There may be only one place
>where you want to know the length of a chain, but I'd still probably
>make it a function.

If you look above where I added more context you will see that the
function is declared as receiving edict_t* ent as its only argument.

[toc] | [prev] | [next] | [standalone]


#150730

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-03-22 16:20 +0000
Message-ID<87zhc85pjq.fsf@bsb.me.uk>
In reply to#150728
Geoff <geoff@invalid.invalid> writes:

> On Sun, 22 Mar 2020 12:00:52 +0000, Ben Bacarisse
> <ben.usenet@bsb.me.uk> wrote:
>
>>Geoff <geoff@invalid.invalid> writes:
<cut>
>>> void SomeFunction(edict_t* ent)
>>> {
>>> 		edict_t* master;	// edict_t contains member chain
>>> 		unsigned count;
>>>
>>> ... some conditions predicating execution of the loop...
>>>
>>> 			count = 0; 
>>> 			for (ent = master; ent; ent = ent->chain)
>>> 				count++;
>>> ...
>>
>>So I meant writing
>>
>> 			for (edict_t *ent = master; ent; ent = ent->chain)
>> 				count++;
>> 
>>But this should be a function, surely?  There may be only one place
>>where you want to know the length of a chain, but I'd still probably
>>make it a function.
>
> If you look above where I added more context you will see that the
> function is declared as receiving edict_t* ent as its only argument.

Yes, I saw.  I fear we are talking at cross purposes.  I meant making
just the counting loop into a function.  Your example shows the loop
amid other code.

Interestingly, if this code were made into a function I would not use a
for loop!  I'd write

  unsigned edict_chain_length(const edict_t *ep)
  {
      unsigned count = 0;
      while (ep) count++, ep = ep->chain;
      return count;
  }

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#150747

FromGeoff <geoff@invalid.invalid>
Date2020-03-22 14:02 -0700
Message-ID<trjf7f9uta1n4r4aavja8s8ls81h3uhpb1@4ax.com>
In reply to#150730
On Sun, 22 Mar 2020 16:20:57 +0000, Ben Bacarisse
<ben.usenet@bsb.me.uk> wrote:

>Geoff <geoff@invalid.invalid> writes:
>
>> On Sun, 22 Mar 2020 12:00:52 +0000, Ben Bacarisse
>> <ben.usenet@bsb.me.uk> wrote:
>>
>>>Geoff <geoff@invalid.invalid> writes:
><cut>
>>>> void SomeFunction(edict_t* ent)
>>>> {
>>>> 		edict_t* master;	// edict_t contains member chain
>>>> 		unsigned count;
>>>>
>>>> ... some conditions predicating execution of the loop...
>>>>
>>>> 			count = 0; 
>>>> 			for (ent = master; ent; ent = ent->chain)
>>>> 				count++;
>>>> ...
>>>
>>>So I meant writing
>>>
>>> 			for (edict_t *ent = master; ent; ent = ent->chain)
>>> 				count++;
>>> 
>>>But this should be a function, surely?  There may be only one place
>>>where you want to know the length of a chain, but I'd still probably
>>>make it a function.
>>
>> If you look above where I added more context you will see that the
>> function is declared as receiving edict_t* ent as its only argument.
>
>Yes, I saw.  I fear we are talking at cross purposes.  I meant making
>just the counting loop into a function.  Your example shows the loop
>amid other code.
>
>Interestingly, if this code were made into a function I would not use a
>for loop!  I'd write
>
>  unsigned edict_chain_length(const edict_t *ep)
>  {
>      unsigned count = 0;
>      while (ep) count++, ep = ep->chain;
>      return count;
>  }

It is amid other code and the construct (for-loop with comma
operators) only appears twice in the entire code base in a single
function. The first instance as shown and in the second instance using
a different control expression. There is no point in making it a
function since it's only used once. It's pre-ANSI code.

[toc] | [prev] | [next] | [standalone]


#150750

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-03-22 21:26 +0000
Message-ID<87tv2g3wuk.fsf@bsb.me.uk>
In reply to#150747
Geoff <geoff@invalid.invalid> writes:

> On Sun, 22 Mar 2020 16:20:57 +0000, Ben Bacarisse
> <ben.usenet@bsb.me.uk> wrote:
>
>>Geoff <geoff@invalid.invalid> writes:
>>
>>> On Sun, 22 Mar 2020 12:00:52 +0000, Ben Bacarisse
>>> <ben.usenet@bsb.me.uk> wrote:
>>>
>>>>Geoff <geoff@invalid.invalid> writes:
>><cut>
>>>>> void SomeFunction(edict_t* ent)
>>>>> {
>>>>> 		edict_t* master;	// edict_t contains member chain
>>>>> 		unsigned count;
>>>>>
>>>>> ... some conditions predicating execution of the loop...
>>>>>
>>>>> 			count = 0; 
>>>>> 			for (ent = master; ent; ent = ent->chain)
>>>>> 				count++;
>>>>> ...
>>>>
>>>>So I meant writing
>>>>
>>>> 			for (edict_t *ent = master; ent; ent = ent->chain)
>>>> 				count++;
>>>> 
>>>>But this should be a function, surely?  There may be only one place
>>>>where you want to know the length of a chain, but I'd still probably
>>>>make it a function.
>>>
>>> If you look above where I added more context you will see that the
>>> function is declared as receiving edict_t* ent as its only argument.
>>
>>Yes, I saw.  I fear we are talking at cross purposes.  I meant making
>>just the counting loop into a function.  Your example shows the loop
>>amid other code.
>>
>>Interestingly, if this code were made into a function I would not use a
>>for loop!  I'd write
>>
>>  unsigned edict_chain_length(const edict_t *ep)
>>  {
>>      unsigned count = 0;
>>      while (ep) count++, ep = ep->chain;
>>      return count;
>>  }
>
> It is amid other code and the construct (for-loop with comma
> operators) only appears twice in the entire code base in a single
> function. The first instance as shown and in the second instance using
> a different control expression. There is no point in making it a
> function since it's only used once.

If there is no problem, then there is no point in making any changes.
It's almost always best to leave working code alone.  If there is a
problem -- a bug, or the need to add complex functionality -- then you
might consider changes.  A making the code clearer by writing a
well-named function would be a change I would consider.

> It's pre-ANSI code.

I doesn't look like pre-ANSI code.  It has one feature from C99 and two
that were introduced in ANSI C.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#150715

FromDavid Brown <david.brown@hesbynett.no>
Date2020-03-22 10:25 +0100
Message-ID<r57auj$ct6$1@dont-email.me>
In reply to#150712
On 22/03/2020 04:31, Ben Bacarisse wrote:
> Geoff <geoff@invalid.invalid> writes:
> 
>> I'm aware of the hazards of using the comma operator in the tested
>> expression but I ran across this example and I think it should be
>> refactored, but how?
>>
>> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
>> 				;
>>
>>
>> My first instinct is to write it:
>>
>> 		for (count = 0; ent; count++)
>> 		{
>> 			ent = master;
>> 			ent = ent->chain;
>> 		}
>>
>> But is this correct?
> 
> No, the equivalent that is closes to your refactor is
> 
>    ent = master;
>    for (count = 0; ent; count++)
>    {
>            ent = ent->chain;
>    }
> 
> but maybe it's better to make all the part of the for loop relate to the
> pointer and move the counting out of the brackets like this:
> 
>    count = 0;
>    for (ent = master; ent; ent = ent->chain)
>    {
>            count++;
>    }

Agreed.  A "for" loop in which the three parts don't match up with each 
other /might/ be correct, but it screams out for a less muddled, 
confusing and hard-to-maintain refactor.

> 
> But I would (a) declare the pointer for this loop, and (b) scratch the
> {}s:
> 
>    int count = 0;
>    for (T *ent = master; ent; ent = ent->chain)
>        count++;
> 

I'd do (a), but certainly not (b).  But that battle will continue long 
after vim and emacs have faded from memory.

[toc] | [prev] | [next] | [standalone]


#150719

FromRichard Damon <Richard@Damon-Family.org>
Date2020-03-22 08:27 -0400
Message-ID<2RIdG.874431$K87.193328@fx46.iad>
In reply to#150712
On 3/21/20 11:31 PM, Ben Bacarisse wrote:
> (b) scratch the {}s:

This is a point of personal preferences, but many people prefer to use a
rule that ALWAYS use the {}, or at least always if you use the next
line, to avoid the risk of latter adding code to what looks to be in the
block but isn't.

Personally, I allow the {} to be omitted only if I an putting a short
simple statement immediately after the if/loop (on the same line), and
as soon as I feel the need to put it on a new line, I add the braces,
and since I use K&R style, that only adds one additional line to the
code above the line I am adding by moving the statement to the new line.

Often the editor will allow me to type the oening { before the statement
and the return and it will add the closing } where I want it.

I won't say you can't skip the {} for a one line block, as the grammar
says you can, but I find that putting them in cuts down my error rate.

[toc] | [prev] | [next] | [standalone]


#150733

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-03-22 16:31 +0000
Message-ID<87o8so5p2l.fsf@bsb.me.uk>
In reply to#150719
Richard Damon <Richard@Damon-Family.org> writes:

> On 3/21/20 11:31 PM, Ben Bacarisse wrote:
>> (b) scratch the {}s:
>
> This is a point of personal preferences,

Of course.  And in the list of things I hate when I have read other
people's code, strictly redundant {}s are /way/ down the list.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#150713

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2020-03-22 00:33 -0400
Message-ID<r56pqa$u4t$1@dont-email.me>
In reply to#150711
On 3/21/20 10:51 PM, Geoff wrote:
> I'm aware of the hazards of using the comma operator in the tested
> expression but I ran across this example and I think it should be
> refactored, but how?

The "tested expression" is normally called the "loop condition". In the
following code, the loop condition is "ent". You are using a comma
operator in both of the other two parts of the for() header, but not in
the condition. There's nothing problematic about the use of the comma
operator in those contexts. You shouldn't be using the comma operator
here, but not for that reason.

> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)

The header of a for loop should only be used for the loop controls. The
variable 'count' isn't part of the loop control, so the two expressions
involving 'count' should be moved elsewhere (which would eliminate your
worries about the comma operator). This isn't a matter of the code being
incorrect - it will work as intended if written this way. This is a
matter of making the code easier to understand.

> My first instinct is to write it:
> 
> 		for (count = 0; ent; count++)
> 		{
> 			ent = master;
> 			ent = ent->chain;
> 		}
> 
> But is this correct?

Ben's already shown you the best way to write this, but he didn't
explain why that version of the code is wrong. That version sets ent =
master at the start of each pass through the loop. As a result, ent is
always equal to master->chain at the end of the loop. Unless
master->chain is null, that will result in an infinite loop that will
cause count to overflow (unless it is unsigned, in which case it will
wrap around). You want to execute ent = master only once. Your first
version does that, and so does Ben's re-write of your second version.

[toc] | [prev] | [next] | [standalone]


#150718

FromBart <bc@freeuk.com>
Date2020-03-22 12:23 +0000
Message-ID<1NIdG.90366$0hk.19720@fx08.am4>
In reply to#150711
On 22/03/2020 02:51, Geoff wrote:
> I'm aware of the hazards of using the comma operator in the tested
> expression but I ran across this example and I think it should be
> refactored, but how?
> 
> 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
> 				;
> 
> 
> My first instinct is to write it:
> 
> 		for (count = 0; ent; count++)
> 		{
> 			ent = master;
> 			ent = ent->chain;
> 		}
> 
> But is this correct?
> 

Most C coders here would still see that as for-loop.

I see it as just a while loop:

     count = 0;
     ent = master;

     while (ent) {
         ++count;
         ent = ent->chain;
     }

[toc] | [prev] | [next] | [standalone]


#150720

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2020-03-22 05:47 -0700
Message-ID<0fc029c2-7036-4ca9-86af-15913f874a08@googlegroups.com>
In reply to#150718
On Sunday, March 22, 2020 at 8:23:33 AM UTC-4, Bart wrote:
> On 22/03/2020 02:51, Geoff wrote:
> > I'm aware of the hazards of using the comma operator in the tested
> > expression but I ran across this example and I think it should be
> > refactored, but how?
> > 
> > 	for (count = 0, ent = master; ent; ent = ent->chain, count++)
> > 				;
> > 
> > 
> > My first instinct is to write it:
> > 
> > 		for (count = 0; ent; count++)
> > 		{
> > 			ent = master;
> > 			ent = ent->chain;
> > 		}
> > 
> > But is this correct?
> > 
> 
> Most C coders here would still see that as for-loop.

All C coders see it as a for-loop. You have very strong preferences for
a language significantly different than C, and let those preferences
interfere with your ability to understand what C actually is. As a
result, you're not really a C coder, just someone who writes C code
without properly understanding it. 

> I see it as just a while loop:
> 
>      count = 0;
>      ent = master;
> 
>      while (ent) {
>          ++count;
>          ent = ent->chain;
>      }

And in precisely the same sense,
    for(int i=0; i<n; i++)
       array[i] = i;

is just a while loop:

    i = 0;
    while(i < n){
        array[i] = i;
        i++;
    }

What you really mean is that the for loops written by the OP don't meet
your preconceived notions of how a for loop should be used. I don't say
this in any hope that it will change your attitude, but only to help the
OP, who is clearly a newbie, avoid being confused by your comments.

[toc] | [prev] | [next] | [standalone]


#150722

FromBart <bc@freeuk.com>
Date2020-03-22 14:17 +0000
Message-ID<SrKdG.15586$E%1.3302@fx31.am4>
In reply to#150720
On 22/03/2020 12:47, James Kuyper wrote:
> On Sunday, March 22, 2020 at 8:23:33 AM UTC-4, Bart wrote:

>> Most C coders here would still see that as for-loop.
> 
> All C coders see it as a for-loop. You have very strong preferences for
> a language significantly different than C,

You seem to be forgetting that C HAS a while-loop.

I'm curious now as to under what circumstances anybody here would ever 
actually use it.


>> I see it as just a while loop:
>>
>>       count = 0;
>>       ent = master;
>>
>>       while (ent) {
>>           ++count;
>>           ent = ent->chain;
>>       }
> 
> And in precisely the same sense,
>      for(int i=0; i<n; i++)
>         array[i] = i;
> 
> is just a while loop:
> 
>      i = 0;
>      while(i < n){
>          array[i] = i;
>          i++;
>      }

You could do that. But languages that have 'while' AND 'for', would have 
introduced 'for' to streamline some common patterns, such as iterating 
over the inclusive range 0 to N-1.

Funnily enough, turning this 'while' into its 'for' version, after 
making adjustments such as using {} for the loop body, declaring 'i' in 
the while loop, and ignoring extraneous white-space, only saves about 
two characters, which can be accounted for by the fact that 'while' is 
two letters longer than 'for'.

That suggests to me even less reason for choosing for over while, so the 
only reason I can see for 'for' being so popular for ANY kind of loop, 
is that people /like/ writing unreadable and confusing for-headers 
instead of proper code.

-------------
for(int i=0;i<n;i++){array[i]=i;}
int i=0;while(i<n){array[i]=i;i++;}
{int i=0;while(i<n){array[i]=i;i++;}}   // make i local
-------------


> What you really mean is that the for loops written by the OP don't meet
> your preconceived notions of how a for loop should be used. I don't say
> this in any hope that it will change your attitude, but only to help the
> OP, who is clearly a newbie, avoid being confused by your comments.

I'm giving my opinion on how the OP's loop could be written. I think my 
version is clearer, which is partly borne out by the fact that the OP's 
interpretation of what their for-loop did was wrong.

It's also clearer in that the purpose of the loop is now much more obvious.

[toc] | [prev] | [next] | [standalone]


#150723

Fromfelix@palmen-it.de (Felix Palmen)
Date2020-03-22 15:55 +0100
Message-ID<irakkg-afr1.ln1@mail.home.palmen-it.de>
In reply to#150722
* Bart <bc@freeuk.com>:
> You seem to be forgetting that C HAS a while-loop.

You seem to be forgetting that this is actually *the same*¹

    statement1;
    while (condition) {
        [...]
        statement2;
    }

is always *exactly* the same as:

    for (statement1; condition; statement2) {
        [...]
    }

*If* "statement1" initializes the control state of the loop and
"statement2" advances it, writing this as a while-loop is just
braindead.

----
¹ except when "statement1" is a definition with initialization, in that
case, only a for-loop can limit the scope of some variable
appropriately.

-- 
 Dipl.-Inform. Felix Palmen  <felix@palmen-it.de>   ,.//..........
 {web}  http://palmen-it.de  {jabber} [see email]   ,//palmen-it.de
 {pgp public key}     http://palmen-it.de/pub.txt   //   """""""""""
 {pgp fingerprint} A891 3D55 5F2E 3A74 3965 B997 3EF2 8B0A BC02 DA2A

[toc] | [prev] | [next] | [standalone]


#150724

FromBart <bc@freeuk.com>
Date2020-03-22 15:01 +0000
Message-ID<N4LdG.57428$WT2.44415@fx06.am4>
In reply to#150723
On 22/03/2020 14:55, Felix Palmen wrote:
> * Bart <bc@freeuk.com>:
>> You seem to be forgetting that C HAS a while-loop.
> 
> You seem to be forgetting that this is actually *the same*¹
> 
>      statement1;
>      while (condition) {
>          [...]
>          statement2;
>      }
> 
> is always *exactly* the same as:
> 
>      for (statement1; condition; statement2) {
>          [...]
>      }

In that case, why favour FOR over WHILE, rather than the other way 
around? Because it's easier to cram more stuff on one line?

The OPs first example had FIVE things going on inside the for-header.

To me that is not good coding practice.

[toc] | [prev] | [next] | [standalone]


#150725

Fromfelix@palmen-it.de (Felix Palmen)
Date2020-03-22 16:05 +0100
Message-ID<4fbkkg-2kr1.ln1@mail.home.palmen-it.de>
In reply to#150724
* Bart <bc@freeuk.com>:
> In that case, why favour FOR over WHILE, rather than the other way 
> around? Because it's easier to cram more stuff on one line?

Didn't you understand the part you cut off in your quote or are you just
trolling?

> The OPs first example had FIVE things going on inside the for-header.
> 
> To me that is not good coding practice.

Ok, probably the latter...

-- 
 Dipl.-Inform. Felix Palmen  <felix@palmen-it.de>   ,.//..........
 {web}  http://palmen-it.de  {jabber} [see email]   ,//palmen-it.de
 {pgp public key}     http://palmen-it.de/pub.txt   //   """""""""""
 {pgp fingerprint} A891 3D55 5F2E 3A74 3965 B997 3EF2 8B0A BC02 DA2A

[toc] | [prev] | [next] | [standalone]


#150734

FromBart <bc@freeuk.com>
Date2020-03-22 16:46 +0000
Message-ID<mDMdG.25483$GQ.24448@fx21.am4>
In reply to#150725
On 22/03/2020 15:05, Felix Palmen wrote:
> * Bart <bc@freeuk.com>:
>> In that case, why favour FOR over WHILE, rather than the other way
>> around? Because it's easier to cram more stuff on one line?
> 
> Didn't you understand the part you cut off in your quote or are you just
> trolling?

I was replying to your assertion (which /you/ have snipped) that those 
two forms were *exactly* the same.

If your exception was that critical and is a factor in the majority of 
such loops, then your assertion was wrong or misleading.

If that is not the case, then my question, for all those other 
situations, still stands.

(BTW you can limit the scope in the while-version in the same way by 
putting the whole thing inside a block.)


> 
>> The OPs first example had FIVE things going on inside the for-header.
>>
>> To me that is not good coding practice.
> 
> Ok, probably the latter...
> 

If you think taking an unrelated assortment of declarations, 
initialisations, expressions and conditional expressions, and sticking 
them all inside a pair of parentheses delimited by a mix of commas and 
semicolons, preferally all on one line, is good practice, then I suspect 
/you're/ the one who's trolling.

See also MISRA 2004 rule 13.5 (don't know if later versions have the 
same rule number):

"The three expressions of a for statement shall be concerned only
with loop control.

The three expressions of a for statement shall be used only for these 
purposes:

First expression Initialising the loop counter (i in the following example)

Second expression Shall include testing the loop counter (i), and 
optionally other
loop control variables (flag)

Third expression Increment or decrement of the loop counter (i)"

Think about why they might have such a rule. And think about what 
structured programming is supposed to mean.

[toc] | [prev] | [next] | [standalone]


#150735

Fromfelix@palmen-it.de (Felix Palmen)
Date2020-03-22 18:14 +0100
Message-ID<90jkkg-s1o1.ln1@mail.home.palmen-it.de>
In reply to#150734
* Bart <bc@freeuk.com>:
> If your exception [...]

I wasn't talking about the exception. Either learn reading entire posts
or drop it.

-- 
 Dipl.-Inform. Felix Palmen  <felix@palmen-it.de>   ,.//..........
 {web}  http://palmen-it.de  {jabber} [see email]   ,//palmen-it.de
 {pgp public key}     http://palmen-it.de/pub.txt   //   """""""""""
 {pgp fingerprint} A891 3D55 5F2E 3A74 3965 B997 3EF2 8B0A BC02 DA2A

[toc] | [prev] | [next] | [standalone]


Page 1 of 6  [1] 2 3 4 5 6  Next page →

Back to top | Article view | comp.lang.c


csiph-web