Loop "Equivalencies"

richw at ada-uts.UUCP richw at ada-uts.UUCP
Thu Nov 14 01:40:00 AEST 1985


I'd like to point out what I think is a bug in Kernighan & Ritchie.
My version's copyright is 1978; please excuse this if I have an old
version and newer versions (?) have fixed it.

In Appendix A (C Reference Manual), p. 202, and p. 56, it says that
the following:

    for (expr1; expr2; expr3)
        statement

is equivalent to:

    expr1;
    while (expr2) {
        statement
        expr3;
    }

This is not true if "statement" is a block which contains a "continue"
since, in the first case, "expr3", is executed after the continue but
is not in the second case.

I came across this while working on someone else's code.  This code
constantly used the "while" form in places where I thought a "for"
was more concise and appropriate.  So, whenever I'd see the second
"while" form, I'd transform it (based on this buggy K&R statement).
Then I got burned...

Am I missing something?

-- Rich Wagner



More information about the Comp.lang.c mailing list