Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!orion.oac.uci.edu!ucivax!milne From: milne@ics.uci.edu (Alastair Milne) Newsgroups: comp.lang.pascal Subject: Re: Overflow error Message-ID: <285B11CC.14632@ics.uci.edu> Date: 16 Jun 91 07:22:52 GMT References: <2bdd60c9e7152849799c@rose.uucp> <2547@cybaswan.UUCP> Distribution: na,uk Organization: UC Irvine Department of ICS Lines: 41 In <2547@cybaswan.UUCP> cslaurie@cybaswan.UUCP (Laurie Moseley ) writes: >Surely any file-reading should be done in a WHILE NOT EOF loop, >rather than a REPEAT UNTIL EOF one. The whole point of the WHILE is >that it may be entered zero times, and therefore you are automatically >guarding against the danger of trying to read from an empty file. In >general the use of WHILE for the traversal of many structures (queues >stacks etc) is good practice anyway, as it removes at a stroke the >need for a lot of special case checking, such as testing for nil pointers. Unfortunately, the scanning of lists or other graphs is not quite as simple as that, but it's a good point that for cases where the loop may never run at all, WHILE is to be preferred. But since we seldom work with empty files, it often takes a moment's thought to remember that it can happen. >I reserve the use of REPEAT UNTIL for cases where I know that the loop >will be entered at least once - keyboard input data checking for >example, or a sort routine. It was invented only for convenience >after all, and can be emulated by a WHILE. I disagree that it was invented for convenience. Rather, it was invented for cases where the semantics of the WHILE loop obscure the fact that the loop must run at least once. How often have we seen such constructs as this:? Done := TRUE; WHILE NOT Done DO BEGIN ... END; which, once you realise what to look for, are begging for conversion into a REPEAT. With cases like this taken into account, I find I use many more REPEATs than WHILEs. [observations about parameterless routines and use of globals deleted -- I agreed with them all anyway!] Alastair Milne