Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

are for loops and while loops the same thing?

0 views
Skip to first unread message

Judith Donath

unread,
Apr 4, 1986, 8:15:09 PM4/4/86
to
K & R, page 56:
"The for statement
for (expr1; expr2; expr3)
statement
is equivalent to
expr 1;
while (expr2) {
statement
expr3;
}"

Not always true.

K & R, page 62
"The continue statement... causes the next iteration of the
enclosing loop (for, while, do) to begin. In the whie and do, this
means that the test part is executed immediately; in the for, control
passes to the re-initialization step."

Thus, a loop like
for (i = 0; i < 4; i++)
{
if (i == 2)
{
printf("continuing\n");
continue;
}
else
printf("%d\n", i);
}
terminates, while

i = 0;
while (i < 4)
{
if (i == 2)
{
printf("continuing\n");
continue;
}
else
printf("%d\n");
i++;
}
loops forever.

Is there any reason or use for this difference in behaviour between
while and for? Are there any implementations in which a continue
in a for loop passes control to the test, as if for was really
the equivalent of the above structured while loop?

Chris Torek

unread,
Apr 6, 1986, 1:30:07 AM4/6/86
to
In article <1...@mit-amt.MIT.EDU> jud...@mit-amt.MIT.EDU (Judith Donath)

[asks yet another `frequently asked' question; perhaps this should be
included in the net.announce.newusers section.]

>K & R, page 56:
> "The for statement
> for (expr1; expr2; expr3)
> statement
> is equivalent to
> expr 1;
> while (expr2) {
> statement
> expr3;
> }"
>
>Not always true.

Correct.

>K & R, page 62
> "The continue statement... causes the next iteration of the

> enclosing loop (for, while, do) to begin. ..."

Note that this is in the `tutorial' part of the book, and that page
62 comes after page 56.

>Is there any reason or use for this difference in behaviour between
>while and for?

This is the way most people believe they should behave.

>Are there any implementations in which a continue in a for loop

>passes control to the test [...] ?

If so, those implementations are incorrect.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP: seismo!umcp-cs!chris
CSNet: chris@umcp-cs ARPA: ch...@mimsy.umd.edu

Gregory Smith

unread,
Apr 7, 1986, 12:36:23 PM4/7/86
to
In article <1...@mit-amt.MIT.EDU> jud...@mit-amt.MIT.EDU (Judith Donath) writes:
[example deleted. I have substituted this from K&R pg. 203]
while(...){ for(...){
... ...
contin:; contin:;
} }
[ Judith pointed out that 'continue' does the re-initialization step on the
for loop ]

>
>Is there any reason or use for this difference in behaviour between
>while and for?

Definitely. The re-initialization step is intended to be part of the loop
control. 'continue' is intended to mean 'abandon this execution of the loop
and go on to the next'. Thus you would want the re-initialization step done,
so the loop control will work properly.
E.g.: execute loop for i==1 to 10, but not for i == skip:

for(i=1; i<=10; ++i ){
if( i==skip) continue;
...
}
If ++i was not done after continue, an infinite loop would result.

>Are there any implementations in which a continue
>in a for loop passes control to the test, as if for was really
>the equivalent of the above structured while loop?

If there are, they are incorrect, and they are in trouble.
--
"If you aren't making any mistakes, you aren't doing anything".
----------------------------------------------------------------------
Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg

0 new messages