Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site mit-amt.MIT.EDU Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!mit-amt!judith From: judith@mit-amt.MIT.EDU (Judith Donath) Newsgroups: net.lang.c Subject: are for loops and while loops the same thing? Message-ID: <139@mit-amt.MIT.EDU> Date: Fri, 4-Apr-86 20:15:09 EST Article-I.D.: mit-amt.139 Posted: Fri Apr 4 20:15:09 1986 Date-Received: Sun, 6-Apr-86 01:10:45 EST Organization: MIT Media Lab, Cambridge, MA Lines: 50 Keywords: for, while, continue 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?