Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!ucsd!chem.ucsd.edu!tps From: tps@chem.ucsd.edu (Tom Stockfisch) Newsgroups: comp.lang.c Subject: Re: TO C OR NOT TO *C Message-ID: <585@chem.ucsd.EDU> Date: 24 Oct 89 01:01:46 GMT References: <16107@nswitgould.cs.uts.oz> <1989Oct16.172249.18387@utzoo.uucp> <991@cirrusl.UUCP> <3775@condict.cs.vu.nl> Reply-To: tps@chem.ucsd.edu (Tom Stockfisch) Organization: Chemistry Dept, UC San Diego Lines: 47 In article <3775@condict.cs.vu.nl> condict@cs.vu.nl (Michael Condict) writes: > Arbitrarily_complicated_stuff_that_does_some_input > while (! feof(stdin)) { > .. do stuff with input .. > Arbitrarily_complicated_stuff_that_does_some_input > } Someone proposed a language extension which handles this very elegantly: do { c = getc(stdin); } while (!feof(stdin)) { .. do stuff with input .. } where the second compound statement is executed only when the "while" test succeeds. When it fails, execution continues after the second compound statement. Until such time as this makes it into a compiler, I just use for (;;) { c = getc(stdin); if (feof(stdin)) /* LOOP TEST */ break; .. do stuff with input .. } I always mark the loop test with an eye-catching comment. Structured programmers please note that there still is only one entrance point and one exit point. >This problem can be gotten around in C, using the ',' operator: > > while (c = getc(stdin), !feof(stdin)) { > .. do stuff with c .. > } This doesn't always work: for instance, if the first part is a statement. On all compilers I work on, there is the additional requirement (the standard not withstanding) that all operands of the comma operator be non-void. -- || Tom Stockfisch, UCSD Chemistry tps@chem.ucsd.edu