[comp.lang.c] for

pardo@uw-june.UUCP (David Keppel) (01/08/88)

>>	for (i=0; i==100; i++) foo(&i);

Did you mean
	for (i=0; i<100; i++) foo(&i);
by any chance?  The first form checks i==100 before calling foo(&i);
if the compiler was smart enough to optimize away the initial check,
it would optimize away the whole loop.

	;-D on  (Correct me if I'm wrong.  Wrong me if I'm correct)  Pardo

dg@lakart.UUCP (David Goodenough) (10/15/88)

From article <6945@cdis-1.uucp>, by tanner@cdis-1.uucp (Dr. T. Andrews):
> In article <4700019@m.cs.uiuc.edu>, kenny@m.cs.uiuc.edu writes:
> ) ...implementors, notably Encore, have been lax about implementing
> ) integer[pointer] and integer +- pointer.  It is unwise to depend on
> ) either form's working in portable code; ...
> 
> It is also possible that compiler writers will get the "for" loop
> handling wrong.  It is unwise to depend on "for" loops in portable
> code.  Use a "while" loop instead.
> -- 
> ...!bikini.cis.ufl.edu!ki4pv!cdis-1!tanner  ...!bpa!cdin-1!cdis-1!tanner
> or...  {allegra killer gatech!uflorida decvax!ucf-cs}!ki4pv!cdis-1!tanner

I agree with Dr. Andrews in part: we use the Greenhills C compiler here,
and it fries it's mind on:

	char a;
	int i = 2;

	a = i["Hello World"];

However I would have thought that any compiler that can't do for loops properly
is gonna die real fast. I personally do not use the a = i["..."]; form
ever, so that is not too much of a problem, but if I have to ditch for loops
I'll go out and buy another compiler.
-- 
	dg@lakart.UUCP - David Goodenough		+---+
							| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@harvard.harvard.edu	  	  +---+

lfm@fpssun.fps.com (Larry Meadows) (10/20/88)

:> In article <4700019@m.cs.uiuc.edu>, kenny@m.cs.uiuc.edu writes:
:> 
:> It is also possible that compiler writers will get the "for" loop
:> handling wrong.  It is unwise to depend on "for" loops in portable
:> code.  Use a "while" loop instead.

Wasn't this a joke?  Everyone seems to be taking it seriously.
-- 
Larry Meadows @ FPS			...!tektronix!fpssun!lfm
					...!nosun!fpssun!lfm

wald-david@CS.YALE.EDU (david wald) (10/21/88)

In article <286@lakart.UUCP> dg@lakart.UUCP (David Goodenough) writes:
>From article <6945@cdis-1.uucp>, by tanner@cdis-1.uucp (Dr. T. Andrews):
>> In article <4700019@m.cs.uiuc.edu>, kenny@m.cs.uiuc.edu writes:
>> ) ...implementors, notably Encore, have been lax about implementing
>> ) integer[pointer] and integer +- pointer.  It is unwise to depend on
>> ) either form's working in portable code; ...
>>
>> It is also possible that compiler writers will get the "for" loop
>> handling wrong.  It is unwise to depend on "for" loops in portable
>> code.  Use a "while" loop instead.
>
>I would have thought that any compiler that can't do for loops properly
>is gonna die real fast...If I have to ditch for loops I'll go out and
>buy another compiler.

A couple of days ago I was reading a 17th century book by a man named
Wilkins, on the creation of a new language and writing system.  Included
in his new punctuation was a symbol for indicating the use of irony.  At
the time I thought this was a silly thing to have in a language, since
the point of irony is to simulate serious statements, and people should
have to realize it on their own.  After reading a few responses to Dr.
Andrews' statement, however....


============================================================================
David Wald                                              wald-david@yale.UUCP
						       waldave@yalevm.bitnet
============================================================================

john@frog.UUCP (John Woods) (10/25/88)

In article <337@lfm.fpssun.fps.com>, lfm@fpssun.fps.com (Larry Meadows)writes:
> :> In article <4700019@m.cs.uiuc.edu>, kenny@m.cs.uiuc.edu writes:
> :> 
> :> It is also possible that compiler writers will get the "for" loop
> :> handling wrong.  It is unwise to depend on "for" loops in portable
> :> code.  Use a "while" loop instead.
> 
> Wasn't this a joke?  Everyone seems to be taking it seriously.

Part of the trouble with irony is that you need to make your statements much
more extreme than reality.  There have been a few people on the net who have
just about complained about all the nonportable C code on the net because
their $0.39 IBM-PC C compiler requires line numbers and comments that start
with REM... (and don't even THINK to tell these people "That isn't a C
compiler," it said "BASIC COMPATIBLE C COMPILER" right on the box!)

To all those who defend broken compilers:  there must come a point where it
ISN'T a C compiler.
-- 
John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, john@frog.UUCP, ...!mit-eddie!jfw, jfw@eddie.mit.edu

	Goooooood Morning Discovery!	-Robin Williams

	Abracadabra, 'press to MECO', America is back in space!

diamond@tkou02.enet.dec.com (diamond@tkovoa) (05/29/90)

In article <1990May25.092150.24611@gdr.bath.ac.uk> exspes@gdr.bath.ac.uk (P E Smee) writes:

>One benefit of 'for(;;)' over 'while (1)' which I haven't seen
>mentioned is that most (in my experience) versions of 'lint' will
>cheerfully accept 'for(;;)' without comment, while giving a 'constant
>in conditional' warning for 'while(1)'.

#ifdef LINT
    for(;;) {
#else
    while(1) {
#endif
    :-)

(Of course lint is broken.  Broken tools were desirable in those days.)

(Follow-ups to comp.lang.c, because this topic does not belong in comp.std.c)
-- 
Norman Diamond, Nihon DEC     diamond@tkou02.enet.dec.com
Proposed group comp.networks.load-reduction:  send your "yes" vote to /dev/null.

diamond@tkou02.enet.dec.com (diamond@tkovoa) (05/30/90)

Followups to comp.lang.c, where most of this belongs.

In article <3093@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:

>I loathe and detest while (1) because it's obfuscatory.
>The thing is, a skilled programmer uses a while (..) statement because
>that's the way it fell out of his preconditions, postconditions,
>invariant, and so on.  You use 'while' to express "here after this
>keyword is all the information you need about when the loop stops."

Exactly the same reasoning applies to the second expression in a
"for" statement.  You use "for" to express "the second expression
after this keyword is all the information you need about when the
loop stops."
(My opinion and argument here are serious, not intended to be satirical.)

>The character sequence (;;) has, I believe, no other use in C.  It's a
>very clear signal "no, it _wasn't_ a mistake, I really _did_ mean to
>put the real loop control somewhere else."

while(1) is an equally clear signal.

>In fact, I would welcome a "lint" option that reported 'break' or
>'continue' statements having a while (..) or do while (..) statement
>as scope as being likely errors.

I loathe and detest all "break" and "continue" statements.  The
language should have been designed differently.  Nonetheless,
"break" and "continue" are suitable for while(1) and for(;;) loops.
Reading becomes difficult when "break" or "continue" occurs in either
a "while" or "for" loop with non-constant terminating condition.

>Um, I've recently had a lot more contact with people from a Pascal
>background than I really wanted.  Quickly now, what's wrong with this:
>	program main;
>	    var i: integer;
>	    procedure p(n: integer);
>		begin
>		    for i := 1 to n do write(' ');
>		    writeln(i);
>		end;
>	    begin
>		p(10);
>	    end.
>(I count three violations of the Pascal Standard.)

Then read the Pascal Standard and count again.  There is one violation.
(Did you think that Pascal lacks a null statement?)

-- 
Norman Diamond, Nihon DEC     diamond@tkou02.enet.dec.com
Proposed group comp.networks.load-reduction:  send your "yes" vote to /dev/null.

sa1z+@andrew.cmu.edu (Sudheer Apte) (10/18/90)

gordon@osiris.cso.uiuc.edu (John Gordon) writes:
>         Also, one other thing: for() and while() loops are essentially 
> identical.  The following loops are exactly the same:
> [ example "for" and "while" loops deleted ]

Yes, but there's one pitfall: the way "continue" is handled.  If you
put a "continue" in a while loop, your incrementing operation at the
end of the loop will never be executed after the "continue" is taken,
making your "while" loop run forever.

Example:

#include <stdio.h>

main()
{
  int i;
  printf("FOR loop: ");
  for (i=0; i<10; i++) {      /* Prints 1 2 3 4 6 7 8 9 */
    if(i==5)
      continue;
    printf(" %d", i);
  }
  printf("\nWHILE loop: ");
  i = 0;
  while (i<10) {
    if (i==5)
      continue;                     /* Bad idea */
    printf(" %d", i); fflush(stdout);
    i++;
  }
  printf("\n");
}


Thanks,
	Sudheer.
------------------------
...{harvard,uunet}!andrew.cmu.edu!sa1z
sa1z%andrew@CMCCVMA