Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!tlg From: tlg@ukc.ac.uk (T.L.Goodwin) Newsgroups: comp.lang.c Subject: For vs while (was Re: Comparing strings...) Message-ID: <2226@ukc> Date: 17 Oct 90 11:14:44 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> Reply-To: tlg@ukc.ac.uk (T.L.Goodwin) Organization: Computing Lab, University of Kent at Canterbury, UK. Lines: 28 Summary: Expires: Sender: Followup-To: In article <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: > >#1) for(i = 0; i < 100; i++) > { > body of loop > } > >#2) i = 0; > while(i < 100) > { > body of loop > i++; > } Almost, but not quite. If the body of the loop contains a "continue", then the second example becomes an infinite loop (other changes to i and "break"s notwithstanding), since the increment is never reached. John is not alone: this equivalence is wrongly claimed in Steve Bourne's book "The UNIX System", and also K&R (2, I haven't got a copy of 1 handy), although Bjarne Stroustrup gets it right in the C++ reference manual. Regards, Tim Goodwin.