richw@ada-uts.UUCP (11/13/85)
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
gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (11/19/85)
> 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. > > Am I missing something? No, you have caught K&R in a misleading statement. The equivalence is only approximate, and continue breaks the strict equivalence. Harbison & Steele (p. 209) have this right.
rjnoe@riccb.UUCP (Roger J. Noe) (11/20/85)
> 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" > > Am I missing something? > -- Rich Wagner Nope, you're exactly right. It should be this way: expr1; while(expr2) { statement contin: expr3; } and should refer to the continue statement a few sections later in appendix A. -- Roger Noe ihnp4!riccb!rjnoe
henry@utzoo.UUCP (Henry Spencer) (11/20/85)
> I'd like to point out what I think is a bug in Kernighan & Ritchie. > ...[equivalence of "for" to "while"]... > 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. > ... > Am I missing something? Only a more up-to-date C Reference Manual, actually. Yes, this was a bug in K&R, and it has been corrected in more recent editions of the CRM. -- Henry Spencer @ U of Toronto Zoology {allegra,ihnp4,linus,decvax}!utzoo!henry
jsdy@hadron.UUCP (Joseph S. D. Yao) (11/20/85)
In article <10200026@ada-uts.UUCP> richw@ada-uts.UUCP writes: >I'd like to point out what I think is a bug in Kernighan & Ritchie. > 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. >Am I missing something? I don't have my K&R with me. I believe that when it got to describing 'continue' ( a f t e r describing these loops), it pointed out that to express 'continue' in the above re- phrasing, you had to replace it with 'goto contin' and add the label 'contin:' before expr3. Obviously, > 1 loop / function means different labels ... too bad cpp didn't rip off a macro assembler for a label generator. ;-) Disclaimer: No, I use goto's about once a year at most. But, read Knuth's "Structured Programming Using Goto's": it i s possible! -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}
devine@asgb.UUCP (Robert J. Devine) (11/21/85)
> 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. > > [ description about C's problem that stems from the "continue" statement > used in loops and the defined equivalence between for loops and while loops ] The problem is noted in "C A Reference Manual" by Harbison and Steele. This 1984 book offers a much better description of current C than K&R does. Much has been added (enums, structure assignment, "void", etc) since 1978. Buy this book! System V also has a more up-to-date document on C, you should take a look at that because it describes AT&T's implementation. And yes, the "continue" ambiguity is still there... Bob