[net.lang.c] break, et cetera

chris@umcp-cs.UUCP (Chris Torek) (11/18/85)

Darn...  when I got up this evening I realized that I had not really
said what I wanted to in the first part of that article.  I hate
to reopen the wound and rub in salt, but . . . here goes:

Given a C construct such as

	for (init; cond; step) {
		code;
		if (cond2)
			break;
		code2;
	}

one can, without changing the meaning of the code, write

	init;
	bool := true;
	while (cond) and bool do
	   begin code;
	   if cond2 then
	      bool := false
	   else
	      begin code2;
	      step
	      end
	   end;

which is how the loop would most likely appear in Pascal.  (The
expanded form if `code' or `code2' contains `continue' statements
is somewhat different, but does exist.)  This expanded form can be
transformed direcly into C in the obvoius way.

These are semantically equivalent when it comes to proving the code
correct.  Since they are equivalent, you may elect to use either
form.  In C, I would use the `compressed' form; but this is a matter
of taste, and you probably know the old expression concerning such.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu

chris@umcp-cs.UUCP (Chris Torek) (11/23/85)

For those who wish to read the `original' article (I have not),
here is the reference:

	Knuth, D. E., `Structured Programming with GOTO Statements',
	_Computing_Surveys_, 6, 4, 1974, 261-301.

The idea is to use `goto' in a very restricted way to express what
the code is supposed to *do*:  For example, if you are searching
for something, and need to do `x' if it is not found, but `y' if
it is, then doing a `goto found' and separating the code for `y'
from the code for the search is quite reasonable.  Others, of
course, will argue that the code for `x' and `y' should be moved
to other routines; but I believe this kind of separation can be
overused, just as can `goto's.

[The information in the next few paragraphs prompted me to add
net.lang.pascal to the Newsgroups line above.  Please stop here
if bored.]

Knuth's usage of `goto' and even `return' may be found in the source
to TeX and MetaFont, which are written in WEB, a language combining
a Pascal preprocessor and a program description/documentation
language.  The `Tangle' processor converts a WEB file to a Pascal
file; and the `Weave' processor converts a WEB to a TeX file.
(Incidentally, there is an interesting bootstrap problem here, as
both Tangle and Weave are also written in WEB.)

This may again be more a personal preference than anything else,
but I find that Knuth's programs written in WEB are considerably
easier to read than many other smaller programs I have seen written
directly in Pascal.  Even the `return' statements---which are
actually `goto's in disguise, and rather thin disguise at that---are
better than the convoluted code with extra booleans found in programs
that steadfastedly avoid the `goto temptation'.

The WEB system is available for 4.1 and 4.2 BSD Unix as part of
the Unix TeX distribution.  (There is a distribution fee, around
$75, I think.)  Contact Pierre MacKay at the University of Washington
for more information:  ARPA: MacKay@WASHINGTON; UUCP:
...!uw-beaver!mackay.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu