Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!dali.cs.montana.edu!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!wuarchive!udel!rochester!pt.cs.cmu.edu!o.gp.cs.cmu.edu!andrew.cmu.edu!sa1z+ From: sa1z+@andrew.cmu.edu (Sudheer Apte) Newsgroups: comp.lang.c Subject: for() and while() (was Re: Comparing strings...) Message-ID: <8b7=x6O00WB487ZnYk@andrew.cmu.edu> Date: 17 Oct 90 20:29:58 GMT References: <2205.271700c2@cc.nu.oz.au> <1990Oct13.190106.15615@ux1.cso.uiuc.edu> <10678.271ade27@amherst.bitnet>, <1990Oct17.030157.460@ux1.cso.uiuc.edu> Organization: Civil Engineering, Carnegie Mellon, Pittsburgh, PA Lines: 40 In-Reply-To: <1990Oct17.030157.460@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes: > Also, one other thing: for() and while() loops are essentially > identical. The following loops are exactly the same: > [ example "for" and "while" loops deleted ] Yes, but there's one pitfall: the way "continue" is handled. If you put a "continue" in a while loop, your incrementing operation at the end of the loop will never be executed after the "continue" is taken, making your "while" loop run forever. Example: #include main() { int i; printf("FOR loop: "); for (i=0; i<10; i++) { /* Prints 1 2 3 4 6 7 8 9 */ if(i==5) continue; printf(" %d", i); } printf("\nWHILE loop: "); i = 0; while (i<10) { if (i==5) continue; /* Bad idea */ printf(" %d", i); fflush(stdout); i++; } printf("\n"); } Thanks, Sudheer. ------------------------ ...{harvard,uunet}!andrew.cmu.edu!sa1z sa1z%andrew@CMCCVMA