are for loops and while loops the same thing?
Judith Donath
judith at mit-amt.MIT.EDU
Sat Apr 5 11:15:09 AEST 1986
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?
More information about the Comp.lang.c
mailing list