[u-u] wow, post-tested loops in sh

D. Hugh Redelmeier hugh at mimosa.com
Mon Jul 4 16:41:10 EDT 2016


| From: Alan J Rosenthal <flaps at 56789.ca>

| >I trust you know about "break" of course?
| 
| Well, yes.  And I have to admit it's hard for me to argue as to why that
| doesn't constitute a post-tested loop.  But to me it doesn't.

I programmed a fair bit in a programming language called Sue.  It was
a systems implementation language inspired by Pascal.  The only loop
it had was like for (;;).  No distinct pre- or post- tested loops.
The only way out was to break (or crash).  It turned out that this was
quite pleasant.

	cycle
	...	break if i != 0;
	end cycle;

Notice the leading ... on the break?  That was provided by the system
and meant that you could find the breaks easily.  Without that, break
can be a little harder to see.

The compiler always formatted the code (pretty-printed).  That was not
intrusive since programs resided on cards and we always looked at the
compiler-produced listing to understand the program.  In other words:
it didn't impose on our choice of editor.

For the first few years that I programmed in C, I indented "break" at
the same level as the control-structure introducer of what was being
broken.

A pre-tested loop is a very very common special case.  A post-tested
loop is an uncommon special case.  A number of people have suggest
that when you see a do-while, you should read the code carefully
because it is error-prone.

Do-while is used in C to guard macro bodies.  This kind of definition:
	#define macro() do { whatever } while (0) /* no final ; */
allows this kind of invocation:
	if (x)
		macro(); /* won't work if macro is just {...} */
	else
		whatever;


More information about the u-u mailing list