2007-02-26

Control flow

Recently I've been reading some articles about control flow and programming language theory in general, which I'm getting more and more interested and I am sharing one issue with you that got me into this.

Maybe you also encountered a situation like this in your programming language:

readSomeFile;
while
( dataLeft )
{

printData;
readSomeFile;
}

Notice the code duplication of the readSomeFile statement. Dirty solutions to this look like:
while( readSomeFile, dataLeft )
{

printData;
}

This works in C++ if readSomeFile is an expression, fails obviously for multiple statements. A Cleaner solution is:
while( true )
{

readSomeFile;
if
( !dataLeft )
break
;
printData;
}
A structure like this is named loop with test in the middle and was proposed as integrated control flow statement for programming languages some 30 years ago by Ole-Johan Dahl and integrated in Ada.
Donald Knuth also discussed statements of that kind in his publication contributing to the goto discussion back then. He also elaborated that one syntactic statement could cover the do-while, while-do, loop-with-middle-test and the endless-loop cases probably improving writability and readability of programs. I wonder why this had not become mainstream since then, but programming languages are still evolving...

No comments: