[net.lang.c] for <==> while

bill@ur-cvsvax.UUCP (Bill Vaughn) (07/11/85)

(Stop me if you've seen this before.)

Section 3.5 of K&R (p. 56) states that the 'for' loop and 'while' loop
can be made equivalent i.e.

					expr1;
for (expr1; expr2; expr3)		while (expr2) {
	statement		<==>		statement
						expr3;
					}

Well ... almost.
As the following program demonstrates there is at least one exception:
If 'statement' contains a 'continue' statement, things may go awry.

Are there any other exceptions?

/*---------------------------------------------------------------------------
 * A program to show a problem with the
 * equivalence between for and while loops.
 */

#include <stdio.h>
#include <signal.h>

int i;

main()
{
	int message(), stop();

	signal(SIGALRM, message);
	signal(SIGINT, stop);

	/*
	 * Here's the for loop ...
	 */
	for (i=0; i<10; i++) {
		if (i > 0)
			continue;
	}

	printf("%d\n",i);
	printf("Hanging ...");
	fflush(stdout);
	alarm(5);

	/*
	 * Here's the equivalent while loop ..
	 * See K&R p. 56.
	 */
	i=0;
	while (i<10) {
		if (i > 0)
			continue;
		i++;
	}
}

stop()
{
	printf("\n%d\n",i);
	printf("The 'continue' statement 'breaks' the for/while equivalence.\n");
	exit(0);
}

message()
{
	printf("\t\tHit your interrupt key to stop this thing.\n");
}

/*----------------------------------------------------------------------*/

Bill Vaughn
{allegra,seismo,decvax}!rochester!ur-cvsvax!bill

mjs@eagle.UUCP (M.J.Shannon) (07/12/85)

> Section 3.5 of K&R (p. 56) states that the 'for' loop and 'while' loop
> can be made equivalent....
> 
> Well ... almost.
> If 'statement' contains a 'continue' statement, things may go awry.

If you look in the reference manual which is contained in K&R, you will see
that the semantics of the continue statement are fully described in terms of
equivalences among while, do-while, and for loops.  Section 9.9 (p. 203) clears
up the `exception' you perceive.  Please bear in mind that the section that you
quoted is part of a tutorial, and so all the gory details aren't presented at
once or in the same place.
-- 
	Marty Shannon
UUCP:	ihnp4!eagle!mjs
Phone:	+1 201 522 6063

gwyn@BRL.ARPA (VLD/VMB) (07/14/85)

There is no problem; "for" is not entirely equivalent to the
corresponding "while" construct, as you have observed.
K&R didn't make this clear, although if you read about
"continue" in Appendix A, the difference becomes apparent.
Later commentators have usually been more careful in declaring
that the "for" loop is equivalent to the corresponding "while",
by adding a caveat about "continue".

stephen@dcl-cs.UUCP (Stephen J. Muir) (07/15/85)

In article <200@ur-cvsvax.UUCP> bill@ur-cvsvax.UUCP (Bill Vaughn) writes:
>Section 3.5 of K&R (p. 56) states that the 'for' loop and 'while' loop
>can be made equivalent i.e.
>
>					expr1;
>for (expr1; expr2; expr3)		while (expr2) {
>	statement		<==>		statement
>						expr3;
>					}
>
>Are there any other exceptions?

Here's another ...
An infinite "for" loop is given by "for (;;) statement".  The equivalent
"while" expression is, logically, "while () statement".  As this fails to
compile, one has to resort to "while (1) statement"; which is less efficient
in most cases as code is generated to test if "1" equals "0".
-- 
UUCP:	...!seismo!mcvax!ukc!dcl-cs!stephen
DARPA:	stephen%lancs.comp@ucl-cs	| Post: University of Lancaster,
JANET:	stephen@uk.ac.lancs.comp	|	Department of Computing,
Phone:	+44 524 65201 Ext. 4599		|	Bailrigg, Lancaster, UK.
Project:Alvey ECLIPSE Distribution	|	LA1 4YR

bill@ur-cvsvax.UUCP (Bill Vaughn) (07/15/85)

> > Section 3.5 of K&R (p. 56) states that the 'for' loop and 'while' loop
> > can be made equivalent....
> > 
> > Well ... almost.
> > If 'statement' contains a 'continue' statement, things may go awry.
> 
> If you look in the reference manual which is contained in K&R, you will see
> that the semantics of the continue statement are fully described in terms of
> equivalences among while, do-while, and for loops. Section 9.9 (p. 203) clears
> up the `exception' you perceive. Please bear in mind that the section that you
> quoted is part of a tutorial, and so all the gory details aren't presented at
> once or in the same place.
> -- 
> 	Marty Shannon

I'm not questioning the sematics of the continue statement in C (the necessity,
... well maybe), but I was simply pointing out that if anyone wanted to change
a 'for loop' to a 'while loop' he/she had better watch out for continue's.
By the way, the exception 'continues' to go unmentioned in section 9.6 and I
cannot see that it is brought up in section 9.9 either. (Maybe it was TOO gory
to bring up. :-)

	Bill Vaughn
	UUCP: {allegra,seismo,decvax}!rochester!ur-cvsvax!bill

ron@brl-tgr.ARPA (Ron Natalie <ron>) (07/16/85)

> Here's another ...
> An infinite "for" loop is given by "for (;;) statement".  The equivalent
> "while" expression is, logically, "while () statement".  As this fails to
> compile, one has to resort to "while (1) statement"; which is less efficient
> in most cases as code is generated to test if "1" equals "0".

MYTH! MYTH! (yeth thailor) most of the popular compilers will evaluate
constant expressions and not test them explicitly.  The 4.2 compiler
even knows enough to tell me that if I don't have an exit from the while(1)
loop, that the further statements are not reached.  It, in fact, generates
the same code for either of the infinite loop cases.

-Ron

mjs@eagle.UUCP (M.J.Shannon) (07/16/85)

> In article <200@ur-cvsvax.UUCP> bill@ur-cvsvax.UUCP (Bill Vaughn) writes:
> >Section 3.5 of K&R (p. 56) states that the 'for' loop and 'while' loop
> >can be made equivalent i.e.
[ elided to save net cost ]
> >Are there any other exceptions?
> 
> Here's another ...
> An infinite "for" loop is given by "for (;;) statement".  The equivalent
> "while" expression is, logically, "while () statement".  As this fails to
> compile, one has to resort to "while (1) statement"; which is less efficient
> in most cases as code is generated to test if "1" equals "0".
> -- 
> UUCP:	...!seismo!mcvax!ukc!dcl-cs!stephen

To reiterate, the specification of the C language (at the time) is in the
Reference Manual, which is at the end of K&R.  The content of the rest of the
book is meant to be a tutorial.  What may seem logical conclusions from the
tutorial are spelled out (or disspelled) in great detail in the Reference
Manual.
-- 
	Marty Shannon
UUCP:	ihnp4!eagle!mjs
Phone:	+1 201 522 6063

guy@sun.uucp (Guy Harris) (07/17/85)

> "while (1) statement"; which is less efficient in most cases as code is
> generated to test if "1" equals "0".

If your compiler generates code to test if "1" equals "0", you don't have a
very good compiler.  The Portable C Compiler generates the same code for
"for(;;)" and "while(1)", and doesn't generate a test for things like
"if(1 == 0)".  I suspect most compilers do the same.

	Guy Harris

bright@dataio.UUCP (Walter Bright) (07/17/85)

Some optimizing compilers generate different code for while and for loops.
The for loop makes it easy for the compiler to figure out what the
loop initialization, continuation and next expressions are. This makes
it easier to do things like loop rotation and converting loops to
do-while loops. An example of a compiler that generates better code
for for loops is the Greenhills C compiler.