Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: while loop vs do while loop reading from the file issue Date: Sat, 10 Dec 2016 16:08:33 -0800 Organization: None to speak of Lines: 57 Message-ID: References: <52f1c174-6af1-4fda-9ff8-a5e2cf507b5f@googlegroups.com> <5d94897b-fedc-4a28-81d0-3579e9bf754d@googlegroups.com> <4d53f1c5-b622-4425-9dfe-694c42676cae@googlegroups.com> <5d68da29-653c-42f5-8dbe-c11d3b304322@googlegroups.com> <87pokz3k5x.fsf@bsb.me.uk> <8737hv36ta.fsf@bsb.me.uk> <87lgvn1obu.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="d0d5c56ca22fa2e528cedb366e6a556f"; logging-data="28874"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199UbuDPI2yGDyCdpvvJ/kt" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:qUUHZCMNkeA7AXa8Voqn0kCNNdk= sha1:Ow6dLw/3y/cTNHxWS0G4pMRYJ6c= Xref: csiph.com comp.lang.c:98748 BartC writes: > On 10/12/2016 21:11, Ben Bacarisse wrote: [...] >> (Have you got an example of a loop that's simpler with pre-EOF testing >> yet? I don't want that interesting point to slip away.) > > This is just going to be your opinion versus mine. Last time this was > discussed, you favoured putting as much as possible into a while > condition; I preferred keeping the conditional minimal, except in the > simplest case (reading a character at a time). > > To me, divorcing the eof check and the main loop terminating logic from > the actual processing that is done, is a benefit: > > while (!eof(f)) { > read stuff... > } I think that's to general to be a useful example. > I can see at a glance that this is probably iterating over a file. I can > see that it will definitely end as soon eof() returns true. > > It's just an idiom I use a lot (in fact I could go through a pile of > code looking for such while loops, in a way I couldn't do if the loop > condition was always different). As I recall, your eof() function attempts to read a character, and the pushes it back with ungetc() if it was able to do so. I haven't tried it, but I believe it would cause problems with interactive input. I remember that being a serious problem with Pascal's input handling. If you don't like putting complex expressions into a while condition, as in the common C idiom: int c; while ((c = getchar()) != EOF) { /* do something with c */ } then you can separate it: while (1) { int c = getchar(); if (c == EOF) break; /* do something with c */ } It's less than ideal in that the condition that terinates the loop is in the body of the loop rather than at the top (which is why I prefer the usual idiom). -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"