The "loop and a half"

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Oct 5 02:57:22 EDT 2017


Steve D'Aprano wrote:
> I recall that the Pascal compiler had to do some clever behind the scenes
> jiggery-pokery to get eof() to work, but that's what compilers are supposed
> to do: make common tasks easy for the programmer.

Sometimes the jiggery-pokery worked, sometimes it didn't.
For example, the following wouldn't work as expected:

    while not eof(input) do begin
       write(output, 'Enter something:');
       readln(input, buffer);
       process(buffer);
    end

because the eof() would block waiting for you to enter
something, so the prompt wouldn't get printed at the
right time.

Basically, Pascal's eof-flag model was designed for
batch processing, and didn't work very well for
interactive use.

Unix and Windows sidestep the issue by not trying to
pretend you can detect EOF independently of reading
data, but using that model gracefully requires a
loop-and-a-half.

-- 
Greg



More information about the Python-list mailing list